Skip to content

Commit 2eb2d67

Browse files
authored
Merge pull request #6631 from commercialhaskell/fix6630
Fix #6630 Also take exectutable file name into account, for '`stack`'
2 parents e089978 + 55afa97 commit 2eb2d67

File tree

4 files changed

+50
-19
lines changed

4 files changed

+50
-19
lines changed

ChangeLog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ Bug fixes:
2121
* Stack's in-app messages refer to https://haskellstack.org as currently
2222
structured. (Most URLs in older Stack versions are redirected.)
2323

24+
* Stack's `upgrade` command only treats the current running Stack executable
25+
as '`stack`' if the executable file is named `stack` or, on Windows,
26+
`stack.exe`. Previously only how it was invoked was considered.
27+
2428
## v3.1.1 - 2024-07-28
2529

2630
Release notes:

doc/commands/upgrade_command.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ By default:
3737
(replacing any existing executable named `stack` there);
3838

3939
* if the current running Stack executable is '`stack`' (that is, it was invoked
40-
as `stack` or, on Windows, `stack.exe` - this is case insensitive), an
41-
existing binary distribution will replace it. If the executable is located
42-
outside of Stack's local binary directory, pass the `--only-local-bin` flag to
43-
skip that step;
40+
as `stack` or, on Windows, `stack.exe` - this is case insensitive - and the
41+
Stack executable file is named `stack` or, on Windows, `stack.exe` - this is
42+
case sensitive), an existing binary distribution will replace it. If the
43+
executable is located outside of Stack's local binary directory, pass the
44+
`--only-local-bin` flag to skip that step;
4445

4546
* if the current running Stack executable is not '`stack`' (as described above),
4647
an existing binary distribution will only be put in Stack's local binary

src/Stack.hs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module Stack
1212
import Control.Monad.Extra ( whenJust )
1313
import GHC.IO.Encoding ( mkTextEncoding, textEncodingName )
1414
import Options.Applicative.Builder.Extra ( execExtraHelp )
15+
import Path ( parseAbsFile )
1516
import Stack.BuildInfo ( versionString' )
1617
import Stack.CLI ( commandLineHandler )
1718
import Stack.Constants ( stackProgName )
@@ -30,7 +31,7 @@ import Stack.Types.Version
3031
, stackVersion
3132
)
3233
import System.Directory ( getCurrentDirectory )
33-
import System.Environment ( getArgs, getProgName )
34+
import System.Environment ( executablePath, getArgs, getProgName )
3435
import System.IO ( hGetEncoding, hPutStrLn, hSetEncoding )
3536
import System.Terminal ( hIsTerminalDeviceOrMinTTY )
3637

@@ -61,6 +62,8 @@ main = do
6162
hSetTranslit stderr
6263
args <- getArgs
6364
progName <- getProgName
65+
mExecutableFilePath <- fromMaybe (pure Nothing) executablePath
66+
let mExecutablePath = mExecutableFilePath >>= parseAbsFile
6467
isTerminal <- hIsTerminalDeviceOrMinTTY stdout
6568
execExtraHelp
6669
args
@@ -73,7 +76,8 @@ main = do
7376
(nixOptsParser False)
7477
("Only showing --" ++ nixCmdName ++ "* options.")
7578
currentDir <- getCurrentDirectory
76-
eGlobalRun <- try $ commandLineHandler currentDir progName False
79+
eGlobalRun <-
80+
try $ commandLineHandler currentDir progName mExecutablePath False
7781
case eGlobalRun of
7882
Left (exitCode :: ExitCode) ->
7983
throwIO exitCode

src/Stack/CLI.hs

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import Options.Applicative.Builder.Extra
1818
( boolFlags, extraHelpOption )
1919
import Options.Applicative.Complicated
2020
( addCommand, addSubCommands, complicatedOptions )
21+
import Path ( filename )
2122
import RIO.NonEmpty ( (<|) )
2223
import qualified RIO.NonEmpty as NE
2324
import qualified RIO.Process ( exec )
@@ -26,7 +27,10 @@ import Stack.Build ( buildCmd )
2627
import Stack.BuildInfo ( hpackVersion, versionString' )
2728
import Stack.Clean ( CleanCommand (..), cleanCmd )
2829
import Stack.ConfigCmd as ConfigCmd
29-
import Stack.Constants ( globalFooter, osIsWindows, stackProgName )
30+
import Stack.Constants
31+
( globalFooter, osIsWindows, relFileStack, relFileStackDotExe
32+
, stackProgName
33+
)
3034
import Stack.Coverage ( hpcReportCmd )
3135
import Stack.Docker
3236
( dockerCmdName, dockerHelpOptName, dockerPullCmdName )
@@ -84,7 +88,7 @@ import Stack.Update ( updateCmd )
8488
import Stack.Upgrade ( upgradeCmd )
8589
import Stack.Upload ( uploadCmd )
8690
import qualified System.Directory as D
87-
import System.Environment ( getProgName, withArgs )
91+
import System.Environment ( withArgs )
8892
import System.FilePath ( pathSeparator, takeDirectory )
8993

9094
-- | Type representing \'pretty\' exceptions thrown by functions in the
@@ -103,9 +107,14 @@ instance Exception CliPrettyException
103107
commandLineHandler ::
104108
FilePath
105109
-> String
110+
-- ^ The name of the current Stack executable, as it was invoked.
111+
-> Maybe (Path Abs File)
112+
-- ^ The path to the current Stack executable, if the operating system
113+
-- provides a reliable way to determine it and where a result was
114+
-- available.
106115
-> Bool
107116
-> IO (GlobalOptsMonoid, RIO Runner ())
108-
commandLineHandler currentDir progName isInterpreter =
117+
commandLineHandler currentDir progName mExecutablePath isInterpreter =
109118
-- Append the relevant default (potentially affecting the LogLevel) *after*
110119
-- appending the global options of the `stack` command to the global options
111120
-- of the subcommand - see #5326.
@@ -136,7 +145,7 @@ commandLineHandler currentDir progName isInterpreter =
136145
parseResultHandler (NE.toList args') f
137146
else
138147
secondaryCommandHandler args' f
139-
>>= interpreterHandler currentDir args'
148+
>>= interpreterHandler progName mExecutablePath currentDir args'
140149
)
141150
(NE.nonEmpty args)
142151
Nothing -> parseResultHandler args f
@@ -516,11 +525,18 @@ commandLineHandler currentDir progName isInterpreter =
516525
\upgrade Stack."
517526
(upgradeOptsParser onlyLocalBins)
518527
where
519-
onlyLocalBins =
520-
(lowercase progName /= lowercase stackProgName)
521-
&& not ( osIsWindows
522-
&& lowercase progName == lowercase (stackProgName <> ".EXE")
523-
)
528+
isProgNameStack =
529+
(lowercase progName == lowercase stackProgName)
530+
|| ( osIsWindows
531+
&& lowercase progName == lowercase (stackProgName <> ".EXE")
532+
)
533+
isRelFileNameStack relFile =
534+
(relFile == relFileStack)
535+
|| (osIsWindows && relFile == relFileStackDotExe )
536+
isExecutableNameStack =
537+
let mExecutableName = filename <$> mExecutablePath
538+
in maybe False isRelFileNameStack mExecutableName
539+
onlyLocalBins = not (isProgNameStack && isExecutableNameStack)
524540
lowercase = map toLower
525541

526542
upload = addCommand'
@@ -669,11 +685,17 @@ secondaryCommandHandler args f =
669685

670686
interpreterHandler ::
671687
Monoid t
672-
=> FilePath
688+
=> String
689+
-- ^ The name of the current Stack executable, as it was invoked.
690+
-> Maybe (Path Abs File)
691+
-- ^ The path to the current Stack executable, if the operating system
692+
-- provides a reliable way to determine it and where a result was
693+
-- available.
694+
-> FilePath
673695
-> NonEmpty String
674696
-> ParserFailure ParserHelp
675697
-> IO (GlobalOptsMonoid, (RIO Runner (), t))
676-
interpreterHandler currentDir args f = do
698+
interpreterHandler progName mExecutablePath currentDir args f = do
677699
-- args can include top-level config such as --extra-lib-dirs=... (set by
678700
-- nix-shell) - we need to find the first argument which is a file, everything
679701
-- afterwards is an argument to the script, everything before is an argument
@@ -711,9 +733,9 @@ interpreterHandler currentDir args f = do
711733
("File does not exist or is not a regular file '" ++ name ++ "'.")
712734

713735
runInterpreterCommand path stackArgs fileArgs = do
714-
progName <- getProgName
715736
iargs <- getInterpreterArgs path
716-
let parseCmdLine = commandLineHandler currentDir progName True
737+
let parseCmdLine =
738+
commandLineHandler currentDir progName mExecutablePath True
717739
-- Implicit file arguments are put before other arguments that
718740
-- occur after "--". See #3658
719741
cmdArgs = prependList stackArgs $ case NE.break (== "--") iargs of

0 commit comments

Comments
 (0)