@@ -18,6 +18,7 @@ import Options.Applicative.Builder.Extra
18
18
( boolFlags , extraHelpOption )
19
19
import Options.Applicative.Complicated
20
20
( addCommand , addSubCommands , complicatedOptions )
21
+ import Path ( filename )
21
22
import RIO.NonEmpty ( (<|) )
22
23
import qualified RIO.NonEmpty as NE
23
24
import qualified RIO.Process ( exec )
@@ -26,7 +27,10 @@ import Stack.Build ( buildCmd )
26
27
import Stack.BuildInfo ( hpackVersion , versionString' )
27
28
import Stack.Clean ( CleanCommand (.. ), cleanCmd )
28
29
import Stack.ConfigCmd as ConfigCmd
29
- import Stack.Constants ( globalFooter , osIsWindows , stackProgName )
30
+ import Stack.Constants
31
+ ( globalFooter , osIsWindows , relFileStack , relFileStackDotExe
32
+ , stackProgName
33
+ )
30
34
import Stack.Coverage ( hpcReportCmd )
31
35
import Stack.Docker
32
36
( dockerCmdName , dockerHelpOptName , dockerPullCmdName )
@@ -84,7 +88,7 @@ import Stack.Update ( updateCmd )
84
88
import Stack.Upgrade ( upgradeCmd )
85
89
import Stack.Upload ( uploadCmd )
86
90
import qualified System.Directory as D
87
- import System.Environment ( getProgName , withArgs )
91
+ import System.Environment ( withArgs )
88
92
import System.FilePath ( pathSeparator , takeDirectory )
89
93
90
94
-- | Type representing \'pretty\' exceptions thrown by functions in the
@@ -103,9 +107,14 @@ instance Exception CliPrettyException
103
107
commandLineHandler ::
104
108
FilePath
105
109
-> 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.
106
115
-> Bool
107
116
-> IO (GlobalOptsMonoid , RIO Runner () )
108
- commandLineHandler currentDir progName isInterpreter =
117
+ commandLineHandler currentDir progName mExecutablePath isInterpreter =
109
118
-- Append the relevant default (potentially affecting the LogLevel) *after*
110
119
-- appending the global options of the `stack` command to the global options
111
120
-- of the subcommand - see #5326.
@@ -136,7 +145,7 @@ commandLineHandler currentDir progName isInterpreter =
136
145
parseResultHandler (NE. toList args') f
137
146
else
138
147
secondaryCommandHandler args' f
139
- >>= interpreterHandler currentDir args'
148
+ >>= interpreterHandler progName mExecutablePath currentDir args'
140
149
)
141
150
(NE. nonEmpty args)
142
151
Nothing -> parseResultHandler args f
@@ -516,11 +525,18 @@ commandLineHandler currentDir progName isInterpreter =
516
525
\upgrade Stack."
517
526
(upgradeOptsParser onlyLocalBins)
518
527
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)
524
540
lowercase = map toLower
525
541
526
542
upload = addCommand'
@@ -669,11 +685,17 @@ secondaryCommandHandler args f =
669
685
670
686
interpreterHandler ::
671
687
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
673
695
-> NonEmpty String
674
696
-> ParserFailure ParserHelp
675
697
-> IO (GlobalOptsMonoid , (RIO Runner () , t ))
676
- interpreterHandler currentDir args f = do
698
+ interpreterHandler progName mExecutablePath currentDir args f = do
677
699
-- args can include top-level config such as --extra-lib-dirs=... (set by
678
700
-- nix-shell) - we need to find the first argument which is a file, everything
679
701
-- afterwards is an argument to the script, everything before is an argument
@@ -711,9 +733,9 @@ interpreterHandler currentDir args f = do
711
733
(" File does not exist or is not a regular file '" ++ name ++ " '." )
712
734
713
735
runInterpreterCommand path stackArgs fileArgs = do
714
- progName <- getProgName
715
736
iargs <- getInterpreterArgs path
716
- let parseCmdLine = commandLineHandler currentDir progName True
737
+ let parseCmdLine =
738
+ commandLineHandler currentDir progName mExecutablePath True
717
739
-- Implicit file arguments are put before other arguments that
718
740
-- occur after "--". See #3658
719
741
cmdArgs = prependList stackArgs $ case NE. break (== " --" ) iargs of
0 commit comments