Skip to content

Commit 6fea3a6

Browse files
authored
Show error message when activate.sh/ps1 is invoked directly instead of sourced (dotnet#7731)
Helps users avoid a common mistake which is otherwise not obvious.
1 parent 9a2db43 commit 6fea3a6

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

activate.ps1

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
#
22
# This file must be used by invoking ". .\activate.ps1" from the command line.
3-
# You cannot run it directly.
3+
# You cannot run it directly. See https://docs.microsoft.com/powershell/module/microsoft.powershell.core/about/about_scripts#script-scope-and-dot-sourcing
4+
#
45
# To exit from the environment this creates, execute the 'deactivate' function.
56
#
67

8+
if ($MyInvocation.CommandOrigin -eq 'runspace') {
9+
Write-Host -f Red "This script cannot be invoked directly."
10+
Write-Host -f Red "To function correctly, this script file must be 'dot sourced' by calling `". $PSCommandPath`" (notice the dot at the beginning)."
11+
exit 1
12+
}
13+
714
function deactivate ([switch]$init) {
815

916
# reset old environment variables

activate.sh

+29-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,35 @@
33
# You cannot run it directly.
44
# To exit from the environment this creates, execute the 'deactivate' function.
55

6+
_RED="\033[0;31m"
67
_MAGENTA="\033[0;95m"
78
_YELLOW="\033[0;33m"
89
_RESET="\033[0m"
910

11+
# This detects if a script was sourced or invoked directly
12+
# See https://stackoverflow.com/a/28776166/2526265
13+
sourced=0
14+
if [ -n "$ZSH_EVAL_CONTEXT" ]; then
15+
case $ZSH_EVAL_CONTEXT in *:file) sourced=1;; esac
16+
THIS_SCRIPT="${0:-}"
17+
elif [ -n "$KSH_VERSION" ]; then
18+
[ "$(cd $(dirname -- $0) && pwd -P)/$(basename -- $0)" != "$(cd $(dirname -- ${.sh.file}) && pwd -P)/$(basename -- ${.sh.file})" ] && sourced=1
19+
THIS_SCRIPT="${0:-}"
20+
elif [ -n "$BASH_VERSION" ]; then
21+
(return 2>/dev/null) && sourced=1
22+
THIS_SCRIPT="$BASH_SOURCE"
23+
else # All other shells: examine $0 for known shell binary filenames
24+
# Detects `sh` and `dash`; add additional shell filenames as needed.
25+
case ${0##*/} in sh|dash) sourced=1;; esac
26+
THIS_SCRIPT="${0:-}"
27+
fi
28+
29+
if [ $sourced -eq 0 ]; then
30+
printf "${_RED}This script cannot be invoked directly.${_RESET}\n"
31+
printf "${_RED}To function correctly, this script file must be sourced by calling \"source $0\".${_RESET}\n"
32+
exit 1
33+
fi
34+
1035
deactivate () {
1136

1237
# reset old environment variables
@@ -38,7 +63,7 @@ deactivate () {
3863
# Cleanup the environment
3964
deactivate init
4065

41-
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
66+
DIR="$( cd "$( dirname "$THIS_SCRIPT" )" && pwd )"
4267
_OLD_PATH="$PATH"
4368
# Tell dotnet where to find itself
4469
export DOTNET_ROOT="$DIR/.dotnet"
@@ -60,10 +85,10 @@ if [ -n "${BASH:-}" ] || [ -n "${ZSH_VERSION:-}" ] ; then
6085
hash -r 2>/dev/null
6186
fi
6287

63-
echo "${_MAGENTA}Enabled the .NET Core environment. Execute 'deactivate' to exit.${_RESET}"
88+
printf "${_MAGENTA}Enabled the .NET Core environment. Execute 'deactivate' to exit.${_RESET}\n"
6489

6590
if [ ! -f "$DOTNET_ROOT/dotnet" ]; then
66-
echo "${_YELLOW}.NET Core has not been installed yet. Run $DIR/restore.sh to install it.${_RESET}"
91+
printf "${_YELLOW}.NET Core has not been installed yet. Run $DIR/restore.sh to install it.${_RESET}\n"
6792
else
68-
echo "dotnet = $DOTNET_ROOT/dotnet"
93+
printf "dotnet = $DOTNET_ROOT/dotnet\n"
6994
fi

docs/BuildFromSource.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,10 @@ Using Visual Studio Code with this repo requires setting environment variables o
101101
Use these command to launch VS Code with the right settings.
102102

103103
On Windows (requires PowerShell):
104-
```
105-
. activate.ps1
104+
```ps1
105+
# The extra dot at the beginning is required to 'dot source' this file into the right scope.
106+
107+
. .\activate.ps1
106108
code .
107109
```
108110

@@ -134,6 +136,8 @@ to make the .NET Core command line tool work well. You can set these environment
134136
On Windows (requires PowerShell):
135137

136138
```ps1
139+
# The extra dot at the beginning is required to 'dot source' this file into the right scope.
140+
137141
. .\activate.ps1
138142
```
139143

0 commit comments

Comments
 (0)