Skip to content

[powershell] Vcpkg sourced powershell scripts don't work in ConstrainedLanguage mode #17890

Open
@Cazadorro

Description

@Cazadorro

Describe the bug
A clear and concise description of what the bug is.

Related (but this problem is way more pervasive than this issue) #5229 though I don't appear to have this issue, the bat file works fine for me.

Vcpkg has run into this issue https://devblogs.microsoft.com/powershell/powershell-constrained-language-mode-and-the-dot-source-operator/

I don't understand powershell and will never touch it, but from what I understand powershell works in a restricted syntactic mode when ConstrainedLanguage is on, which I guess is configured by default for non-privledged accounts in some companies, and when TEMP or TMP is used ie, AppData/Local/Temp (appdata, the output directory does not allow for unconstrained usage of powershell.

So I don't have a way to waive this ConstrainedLanguage mode with out going to admin mode or changing TEMP and TMP user environmental variables, and as you'll see, this straight up breaks everything with vcpkg.

Environment

  • OS: Windows
  • Compiler: Any MSVC

To Reproduce
Steps to reproduce the behavior:

  1. Create a CMake based project
  2. Use -DCMAKE_TOOLCHAIN_FILE=[pathtovcpkg]/vcpkg/scripts/buildsystems/vcpkg.cmake
  3. Example:
"[pathtocmake]/cmake.exe" -DCMAKE_BUILD_TYPE=Debug -DCMAKE_TOOLCHAIN_FILE=[pathtovcpkg]/vcpkg/scripts/buildsystems/vcpkg.cmake -G "CodeBlocks - NMake Makefiles" [projectdir]\myproject
-- The C compiler identification is MSVC 19.28.29337.0
-- The CXX compiler identification is MSVC 19.28.29337.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - failed
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/MSVC/14.28.29333/bin/Hostx64/x64/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/MSVC/14.28.29333/bin/Hostx64/x64/cl.exe - broken
CMake Error at [cmakepath]/cmake-3.19/Modules/CMakeTestCCompiler.cmake:66 (message):
  The C compiler

    "C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/MSVC/14.28.29333/bin/Hostx64/x64/cl.exe"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: [projectpath]/myproject/cmake-build-debug/CMakeFiles/CMakeTmp
    
    Run Build Command(s):nmake /nologo cmTC_ddd2b\fast && 	"C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.28.29333\bin\HostX64\x64\nmake.exe"  -f CMakeFiles\cmTC_ddd2b.dir\build.make /nologo -L                  CMakeFiles\cmTC_ddd2b.dir\build
    Building C object CMakeFiles/cmTC_ddd2b.dir/testCCompiler.c.obj
    	C:\PROGRA~2\MIB055~1\2019\PROFES~1\VC\Tools\MSVC\1428~1.293\bin\Hostx64\x64\cl.exe @[myuser]AppData\Local\Temp\nm6846.tmp
    testCCompiler.c
    Linking C executable cmTC_ddd2b.exe
    	"[cmakepath]\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\cmTC_ddd2b.dir --rc=C:\PROGRA~2\WI3CF2~1\10\bin\100183~1.0\x64\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\100183~1.0\x64\mt.exe --manifests -- C:\PROGRA~2\MIB055~1\2019\PROFES~1\VC\Tools\MSVC\1428~1.293\bin\Hostx64\x64\link.exe /nologo @CMakeFiles\cmTC_ddd2b.dir\objects1.rsp @[userpath]\AppData\Local\Temp\nm6885.tmp
    	C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noprofile -executionpolicy Bypass -file [vcpkgpath]/vcpkg/scripts/buildsystems/msbuild/applocal.ps1 -targetBinary [projectpath]/myproject/cmake-build-debug/CMakeFiles/CMakeTmp/cmTC_ddd2b.exe -installedDir [vcpkgpath]/vcpkg/installed/x64-windows/debug/bin -OutVariable out
    [vcpkgpath]\vcpkg\scripts\buildsystems\msbuild\applocal.ps1 : Cannot dot-source this 
    command because it was defined in a different language mode. To invoke this command without importing its contents, 
    omit the '.' operator.
        + CategoryInfo          : InvalidOperation: (:) [applocal.ps1], NotSupportedException
        + FullyQualifiedErrorId : DotSourceNotSupported,applocal.ps1
     
    NMAKE : fatal error U1077: 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe' : return code '0x1'
    Stop.
    NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.28.29333\bin\HostX64\x64\nmake.exe"' : return code '0x2'
    Stop.
    
  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt:2 (project)


-- Configuring incomplete, errors occurred!
See also "[projectpath]/myproject/cmake-build-debug/CMakeFiles/CMakeOutput.log".
See also "[projectpath]/myproject/cmake-build-debug/CMakeFiles/CMakeError.log".

This same thing also happens when building some dependencies, for example I ran into this with "libconv" (though it is hidden behind another log file)

CMake Error at scripts/cmake/vcpkg_execute_required_process.cmake:105 (message):
    Command failed: [vcpkgdir]vcpkg/downloads/tools/powershell-core-7.1.3-windows/pwsh.exe -noprofile -executionpolicy Bypass -nologo -file [vcpkgdir]vcpkg/scripts/buildsystems/msbuild/applocal.ps1 -targetBinary [vcpkgdir]vcpkg/packages/libiconv_x64-windows/tools/libiconv/bin/iconv.exe -installedDir [vcpkgdir]vcpkg/packages/libiconv_x64-windows/bin
    Working Directory: [vcpkgdir]vcpkg
    Error code: 1
    See logs for more information:
      [vcpkgdir]\vcpkg\buildtrees\libiconv\copy-tool-dependencies-err.log

Call Stack (most recent call first):
  scripts/cmake/vcpkg_copy_tool_dependencies.cmake:34 (vcpkg_execute_required_process)
  scripts/cmake/vcpkg_copy_tool_dependencies.cmake:44 (search_for_dependencies)
  ports/libiconv/portfile.cmake:40 (vcpkg_copy_tool_dependencies)
  scripts/ports.cmake:142 (include)


Error: Building package libiconv:x64-windows failed with: BUILD_FAILED
Please ensure you're using the latest portfiles with `.\vcpkg update`, then
submit an issue at https://github.com/Microsoft/vcpkg/issues including:
  Package: libiconv:x64-windows
  Vcpkg version: 2021-05-05-9f849c4c43e50d1b16186ae76681c27b0c1be9d9

Additionally, attach any relevant sections from the log files above.

Here is the corresponding text found in [vcpkgdir]\vcpkg\buildtrees\libiconv\copy-tool-dependencies-err.log :

�[91mapplocal.ps1: �[91mCannot dot-source this command because it was defined in a different language mode. To invoke this command without importing its contents, omit the '.' operator.�[0m

Expected behavior
This not to be an issue

Failure logs
see above

Additional Context

Two things fix this on the user end, at least for me, Launching everything in admin mode (not a real workable solution) and changing TMP and TEMP environmental variables to a place my non admin account has write access to (Not workable on a system scale, I don't know what the side effects of this are for all future programs). I'm guessing either the powershell scripts need to be fixed, removed, or we need to be able to specify temp for just vcpkg, which if possible, does not appear to be easily visible how to do so.

Metadata

Metadata

Labels

category:vcpkg-bugThe issue is with the vcpkg system (including helper scripts in `scripts/cmake/`)

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions