Skip to content

Commit ee20e91

Browse files
authored
Merge pull request #6745 from commercialhaskell/fix6744
Fix #6744 Use system temp dir when on dest drive
2 parents a4bf14c + ecb8f58 commit ee20e91

File tree

3 files changed

+32
-17
lines changed

3 files changed

+32
-17
lines changed

ChangeLog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ Bug fixes:
2929
environment variable. In previous versions of Stack, this affected
3030
`stack script` when copying a pre-compiled package from another package
3131
database.
32+
* On Windows, when decompressing, and extracting tools, from archive files,
33+
Stack uses the system temporary directory, rather than the root of the
34+
destination drive, if the former is on the destination drive.
3235

3336
## v3.5.1 - 2025-03-29
3437

doc/commands/setup_command.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,13 @@ and the command are inconsistent and take no action.
103103

104104
Stack uses the 7-Zip tool to decompress, and extract tools from, downloaded
105105
archive files. Aiming to avoid long file paths, Stack does so in a temporary
106-
directory (named `stack-tmp-<hash>`) in the root of the drive of the final
107-
destination. Consequently, Stack needs permission to create a directory in
108-
that location. A Windows user account of type Administrator will have
109-
permission to create a directory in the root of the system drive (`C:\`, by
110-
convention) but a Standard user account may well not have permission.
106+
directory (named `stack-tmp-<hash>`) on the drive of the final destination
107+
(either in the system temporary directory, where applicable, or the root of
108+
the drive). Consequently, Stack needs permission to create a directory in
109+
that location.
110+
111+
A Windows user account will usually have permission to create a directory in
112+
the system temporary directory. A Windows user account of type Administrator
113+
will have permission to create a directory in the root of the system drive
114+
(`C:\`, by convention) but a Standard user account may well not have
115+
permission.

src/Stack/Setup.hs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ import Path.CheckInstall ( warnInstallSearchPathIssues )
8282
import Path.Extra ( toFilePathNoTrailingSep )
8383
import Path.IO
8484
( canonicalizePath, doesFileExist, ensureDir, executable
85-
, getPermissions, ignoringAbsence, listDir, removeDirRecur
86-
, removeFile, renameDir, renameFile, resolveFile'
87-
, withTempDir
85+
, getPermissions, getTempDir, ignoringAbsence, listDir
86+
, removeDirRecur, removeFile, renameDir, renameFile
87+
, resolveFile', withTempDir
8888
)
8989
import RIO.List
9090
( headMaybe, intercalate, intersperse, isPrefixOf
@@ -2454,19 +2454,26 @@ withUnpackedTarball7z name si archiveFile archiveType destDir = do
24542454
Nothing -> prettyThrowIO $ TarballFileInvalid name archiveFile
24552455
Just x -> parseRelFile $ T.unpack x
24562456
run7z <- setup7z si
2457-
-- We use a short name for the temporary directory to reduce the risk of a
2458-
-- filepath length of more than 260 characters, which can be problematic for
2459-
-- 7-Zip even if Long Filepaths are enabled on Windows.
2457+
-- We aim to reduce the risk of a filepath length of more than 260 characters,
2458+
-- which can be problematic for 7-Zip if Windows is not 'long file paths'
2459+
-- enabled. We use a short name for the temporary directory ...
24602460
let tmpName = "stack-tmp"
24612461
destDrive = takeDrive destDir
24622462
ensureDir (parent destDir)
2463+
tempDrive <- takeDrive <$> getTempDir
2464+
-- We use a temporary directory with likely a short absolute path ...
2465+
let withTempDir' = if tempDrive == destDrive
2466+
then
2467+
-- We use the system temporary directory if we can, as a Standard user
2468+
-- may well not have permission to create a directory in the root of
2469+
-- the system drive.
2470+
withSystemTempDir
2471+
else
2472+
-- Otherwise we use a temporary directory in the root of the
2473+
-- destination drive.
2474+
withTempDir destDrive
24632475
withRunInIO $ \run ->
2464-
-- We use a temporary directory in the same drive as that of 'destDir' to
2465-
-- reduce the risk of a filepath length of more than 260 characters, which can
2466-
-- be problematic for 7-Zip even if Long Filepaths are enabled on Windows. We
2467-
-- do not use the system temporary directory as it may be on a different
2468-
-- drive.
2469-
withTempDir destDrive tmpName $ \tmpDir ->
2476+
withTempDir' tmpName $ \tmpDir ->
24702477
run $ do
24712478
liftIO $ ignoringAbsence (removeDirRecur destDir)
24722479
run7z tmpDir archiveFile

0 commit comments

Comments
 (0)