node-gyp icon indicating copy to clipboard operation
node-gyp copied to clipboard

"Add-Type : Item has already been added." error when build on windows

Open adamhj opened this issue 4 years ago • 8 comments

  • Node Version: v13.14.0
  • Platform:

OS Name: Microsoft Windows 7 OS Version: 6.1.7601 Service Pack 1 Build 7601 System Type: x64-based PC

  • Compiler:

msbuild: 15.9.20.62856 cl: Microsoft (R) C/C++ Optimizing Compiler Version 19.16.27024.1 for x86

I'm not sure if this is a bug of node-gyp or bug of windows powershell. I've wasted a lot of time to find what's the problem here and finally found it, so I'd like to share something here so someone may find it helpful.

Similar issue: https://github.com/nodejs/node-gyp/issues/2062

First, I'm using visual studio code to write develop js code, and it is configured to use bash shell from msys2 instead of cmd, and this is where the problem comes from.

I'm learning electron, copy & paste some sample code from Internet, generated the package file using electron-forge, and I also add dependency of sqlite3 into my project to test is native code works correct. I also add some environment values for VS path and version.

Then when I npm run make, I get:

gyp ERR! find VS msvs_version was set from command line or npm config
gyp ERR! find VS - looking for Visual Studio version 2017
gyp ERR! find VS running in VS Command Prompt, installation path is:
gyp ERR! find VS "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community"
gyp ERR! find VS - will only use this version
gyp ERR! find VS could not use PowerShell to find Visual Studio 2017 or newer, try re-running with '--loglevel silly' for more details

then npm run make --loglevel silly:

gyp verb find VS "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community"
gyp verb find VS - will only use this version
gyp sill find VS Running C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe [
gyp sill find VS   '-ExecutionPolicy',
gyp sill find VS   'Unrestricted',
gyp sill find VS   '-NoProfile',
gyp sill find VS   '-Command',
gyp sill find VS   "&{Add-Type -Path 'D:\\Develop\\Javascript\\electron_test\\node_modules\\electron-rebuild\\node_modules\\node-gyp\\lib\\Find-VisualStudio.cs';[VisualStudioConfiguration.Main]::PrintJson()}"
gyp sill find VS ]
⠦ Preparing native dependencies: 0 / 1gyp sill find VS PS stderr = {{Some error info with non-displaying characters, see below}}
gyp sill find VS PS stdout = {"type":"Buffer","data":[]}
gyp sill find VS SyntaxError: Unexpected end of JSON input
gyp sill find VS     at JSON.parse (<anonymous>)
gyp sill find VS     at VisualStudioFinder.parseData (D:\Develop\Javascript\electron_test\node_modules\electron-rebuild\node_modules\node-gyp\lib\find-visualstudio.js:171:21)
gyp sill find VS     at D:\Develop\Javascript\electron_test\node_modules\electron-rebuild\node_modules\node-gyp\lib\find-visualstudio.js:142:14
gyp sill find VS     at ChildProcess.exithandler (child_process.js:294:7)
gyp sill find VS     at ChildProcess.emit (events.js:315:20)
gyp sill find VS     at ChildProcess.EventEmitter.emit (domain.js:485:12)
gyp sill find VS     at maybeClose (internal/child_process.js:1026:16)
gyp sill find VS     at Process.ChildProcess._handle.onexit (internal/child_process.js:286:5)
gyp sill find VS  SyntaxError: Unexpected end of JSON input
gyp sill find VS     at JSON.parse (<anonymous>)
gyp sill find VS     at VisualStudioFinder.parseData (D:\Develop\Javascript\electron_test\node_modules\electron-rebuild\node_modules\node-gyp\lib\find-visualstudio.js:171:21)
gyp sill find VS     at D:\Develop\Javascript\electron_test\node_modules\electron-rebuild\node_modules\node-gyp\lib\find-visualstudio.js:142:14
gyp sill find VS     at ChildProcess.exithandler (child_process.js:294:7)
gyp sill find VS     at ChildProcess.emit (events.js:315:20)
gyp sill find VS     at ChildProcess.EventEmitter.emit (domain.js:485:12)
gyp sill find VS     at maybeClose (internal/child_process.js:1026:16)
gyp sill find VS     at Process.ChildProcess._handle.onexit (internal/child_process.js:286:5)
gyp verb find VS could not use PowerShell to find Visual Studio 2017 or newer, try re-running with '--loglevel silly' for more details

Some error happens, and there are some non-displaying characters in the error message, it seems there is some encoding problem here as my OS language is not english, powershell print error message use my locale while js decode it as utf8. It's not important, I spend some time to modify find-visualstudio.js to get the correct error message, it's like this:

Add-Type : Item has already been added. Key in dictionary: 'path'  Key being ad
ded: 'path'
At line:1 char:11
+ &{Add-Type <<<<  -Path 'D:\Develop\Javascript\electron_test\node_modules\elec
tron-rebuild\node_modules\node-gyp\lib\Find-VisualStudio.cs';[VisualStudioConfi
guration.Main]::PrintJson()}
    + CategoryInfo          : NotSpecified: (:) [Add-Type], ArgumentException
    + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Co 
   mmands.AddTypeCommand
 
Unable to find type [VisualStudioConfiguration.Main]: make sure that the assemb
ly containing this type is loaded.
At line:1 char:165
+ &{Add-Type -Path 'D:\Develop\Javascript\electron_test\node_modules\electron-r
ebuild\node_modules\node-gyp\lib\Find-VisualStudio.cs';[VisualStudioConfigurati
on.Main] <<<< ::PrintJson()}
    + CategoryInfo          : InvalidOperation: (VisualStudioConfiguration.Mai 
   n:String) [], RuntimeException
    + FullyQualifiedErrorId : TypeNotFound

This troubles me a lot. I thought the "path" referred in the error message is the "-Path" parameter in the powershell command line, I searched alot and tried many thing to find what the problem is, until I run npm run make in msys shell directly, instead of running it in VSC terminal, I got a new error:

Add-Type : Item has already been added. Key in dictionary: 'tmp'  Key being added: 'tmp'

Then I realized that the conflict key comes from environment variables:

$ env|grep tmp
tmp=C:\Users\MyUserName\AppData\Local\Temp
TEMP=/tmp
TMP=/tmp

Bash environment variables are case sensitive, while powershell's are not, so when powershell reads environment variables from node.js process which is spawned from bash, error occurs.

Running npm run make from cmd.exe works without any problem.

adamhj avatar May 04 '21 16:05 adamhj

test-ps.zip

I've wrote a simple script to reproduce this problem step to reproduce:

  1. modify the script, correct csFile to Find-VisualStudio.cs on your system
  2. run node test-ps.js in msys2 bash shell
  3. check test-ps-err.log file for the error message
  4. you may remove conflicting environment variables and re-run the script

adamhj avatar May 04 '21 16:05 adamhj

The whole log is quite useful. Windows 7 is long dead and unsupported. Is this also Python 2?

cclauss avatar May 04 '21 17:05 cclauss

I have multiple set of python on my machine. Python 3.8 is installed in msys2 environment with pacman. I also have python 2.7 and 3.7 installed from windows installer, both are in PATH, while the 2.7 version is before the 3.7 version.

But I think this has nothing to do with my problem. I have checked powershell processes created in msys and cmd with procmon, both have the same command line, same working directory, they only different in environment variables.

adamhj avatar May 04 '21 20:05 adamhj

The whole log, please. Do you have the possibility to upgrade to a supported operating system?

cclauss avatar May 05 '21 05:05 cclauss

Here is the full log message from msys shell. error-msys.txt

adamhj avatar May 05 '21 10:05 adamhj

@adamhj Did you solve it? I have the same problem

kefeibuhuifei avatar Dec 28 '21 02:12 kefeibuhuifei

it's really old windows and node versions, @adamhj do you still see this happening? @kefeibuhuifei do you still have this problem? If yes, what windows version do you have?

shnooshnoo avatar Nov 28 '23 13:11 shnooshnoo

This issue has been stale for too long (even after a ping last November), plus it's on an old Node version. Will close on Monday unless someone objects by then.

StefanStojanovic avatar Apr 05 '24 09:04 StefanStojanovic