cortex-debug icon indicating copy to clipboard operation
cortex-debug copied to clipboard

Problem with serialport module

Open vankxr opened this issue 6 years ago • 47 comments

I am trying to get SWO output with OpenOCD using an FT2232H chip as a debugger. I am using the second interface of that chip as a fast UART to receive ITM data.

Currently, this extension reports a problem with serialport not being loaded due to a VSCode update. After some research I found out that I need to update the binary_modules. My VS Code is running on Node 12.4.0 and Electron 6.1.5, which is way newer than the version provided.

I tried updating my binary_modules but without success. The best I could get was NODE_VERSION 72, and it requires 73, I assume it has something to do with the Electron version.

Can anyone update the binary_modules or give me advice on how to do it myself?

Thank you

vankxr avatar Feb 05 '20 14:02 vankxr

Maybe you can refer this issue: permanent Fix for the serialport problem

HoriLiu avatar Feb 06 '20 03:02 HoriLiu

OMG, @Marus would probably so glad if it works.

I try to contribute but this is something I don't know how to do and sounds like a super hassle.

haneefdm avatar Feb 06 '20 04:02 haneefdm

I managed to generate a compatible version of the serialport module, using NVM, installing electron 6.1.5 and electron-rebuild, but there was another problem preventing this line of code from working properly, due to webpack.

After packing the code into the dist/extension.js file, the module global variable was being shortened and replaced by a local copy. When the binary_modules normalized path was being spliced, it was infact being spliced into the local copy instead of the global module search path, preventing the next require from finding the serialport module.

I fixed it by creating a symbolic link inside the dist folder, like so: ln -s ../binary_modules/v12.4.0/linux/x64/node_modules node_modules since that folder is already part of the module search path

(opening extension.js with an editor, searching for that part of the code and replacing the local copy, generated by webpack, with module.path also works)

It's a ghetto fix, but it's working now.

vankxr avatar Feb 06 '20 13:02 vankxr

@vankxr Could you help me go through the exact steps you went through to create a new version? Maybe I can push an updated serialport with the next release that was supposed to be this week. I will see about automating it for all platforms.

This is an old issue #85

haneefdm avatar Feb 06 '20 16:02 haneefdm

I think I figured it out. I created a document for future reference on how to do it and also added a unit test.

@Marus, I hope you can answer this.

The darwin directory with v8.9.3 was 3232K, but with v12.4.0 it is 5436K. I am not sure why it is so much bigger. Is this okay? I have already pruned it for production purposes. The new modules are in v12.4.0

buffer-alloc
buffer-alloc-unsafe
buffer-fill
isarray
os-homedir
readable-stream
string_decoder
to-buffer
xtend

Also, I am not sure how this works. About dependencies, buffer-alloc for is also a dependency for Cortex-Debug, so shouldn't we find it already because it would be in the module-path?

Also, we add the binary_modules to the beginning of the path array which means it now overrides modules the system already has?

haneefdm avatar Feb 07 '20 10:02 haneefdm

@haneefdm I can check my bash history, but basically I installed NVM so I could easily select the NodeJS version I wanted (the one my VSCode was running). Then I installed the Electron version my VSCode was using, with npm i -g [email protected], and also npm i -g electron-rebuild After that I created a "dummy" package.json by doing npm init and accepting the defaults with Enter until the end. After that I installed node-serialport, with npm i --save serialport, and finallly electron-rebuild --module-dir ..

At the end I was left with a node_modules folder containing the correct binding binaries corresponding to NODE_VERSION 73 (aka NodeJS 12.4.0 and Electron 6.1.5).

I just deleted the package.json file as it was only used to save serialport so electron-rebuild could know what to rebuild, and then I copied the node_modules folder to ~/.vscode/extensions/marus25.cortex-debug-0.3.4/binary_modules/v12.4.0/linux/x64/.

After that I cd ~/.vscode/extensions/marus25.cortex-debug-0.3.4/dist and ln -s ../binary_modules/v12.4.0/linux/x64/node_modules node_modules

vankxr avatar Feb 07 '20 13:02 vankxr

@vankxr My process was a bit different. I started from a different repo which avoided the global install of electron. I was missing the dummy project part but pruning probably did the same thing. Following is my doc.

Serial Port Binary Module

Manual creation of serialport binary dependency. Intended for developers and not end-users.

  • Clone the serial port
git clone https://github.com/serialport/electron-serialport.git
cd electron-serialport
  • Edit package.json to the particular electron version we want. Only edit the electron package. Leave other packages the way they are.
"devDependencies": {
    "electron": "6.1.5",
    ...
},
  • Run the following commands
npm install
# A window opens and gives you the exact <node-version> that electron is built with.
# You will need this later.
npm start
# get rid of development dependencies
npm prune --production`
# No you have a `node_modules` directory that is ready to be deployed

Depending on the OS/platform/etc., copy/replace the node_modules directory to the appropriate location in the cortex-debug repo

  • MacOS: binary_modules/<node-version>/darwin/x64/node_modules

haneefdm avatar Feb 07 '20 15:02 haneefdm

Now I want to see if I can automate this.

haneefdm avatar Feb 07 '20 15:02 haneefdm

But isn't there a way of properly installing the module when VSCode installs the extension? I mean, if serialport is a problem because it needs native bindings, then all modules that do require that will be a problem...

vankxr avatar Feb 07 '20 15:02 vankxr

It is pretty risky to do it at the user desktop, lots of things can fail. It is my end goal though.

The serial port module has to be compiled and linked with the exact electron version needed. C++ compilation is involved so. The electron version of VSCode does not change very frequently. Having said that, VSCode is about to bump electron to 6.1.6 with plans to move to 7. When they first moved to 6, they had hundreds of problems.

If I can't automate it, I can't get to the end goal either. I am taking baby steps. If it was easy, @Marus would have done it a long time ago. If you have ideas, let me know.

haneefdm avatar Feb 07 '20 16:02 haneefdm

I understand that compilling at the user desktop can be risky. But this is not sustainable IMO, even though VSCode only bumps Electron/Node versions from time to time.

It has to exist a way of dealing with Node modules with native bindings, like prebuild-install

vankxr avatar Feb 07 '20 16:02 vankxr

Yes - binary modules have been a pain since the beginning of the project.

Currently we don't have the ability to install node modules at install/run-time - only at build-time; simply a limitation of VSCode (there had been some talk in the past of enabling this - but has gone nowhere to my knowledge). And of course since we are talking binary modules we can't have a single copy that covers all the platforms that the extension is used on. Which is why currently I have to manually build and bundle (and have special loading code for) the binary modules - as I have to bundle multiple copies (at least 32 bit is dying out so it is a few less variants that need to be included now).

I have been investigating some things to at least automate the building of the modules - to make my process simpler - and do have some thoughts on even possible ways so they wouldn't have to be pre-bundled (although that's less important to me).

But if you have thoughts - or something working for the automation of building the modules on all platforms involved - then would love to see that as well.

Unfortunately something like prebuild-install doesn't work easily either - as there isn't a good way to build them cross platform for Linux, Windows and macOS all on one machine (at least not easily).

Marus avatar Feb 07 '20 18:02 Marus

@Marus I am pretty close to a script to create the serialport binary module. I need some more time. I know @Marus will find a better way. I still think we have to build, deploy and release for the basic install.

One doubt I had was if we have to support previous releases of vscode/node/electron. I am unclear about VSCode roadmap wrt. electron and how far back we have to maintain compatibility

Finally, I don't want to do it at the user's machine during or after the extension install/activation. Sooo many things can go wrong in an uncontrolled environment and not everyone needs it.

Thoughts?

haneefdm avatar Feb 07 '20 20:02 haneefdm

I don't see why would it be a problem to "force" users to update to latest VSCode to keep using this extension (thus removing the need to worry about previous versions).

I personally never encountered anything worth keeping VSCode from updating.

By the way, any thoughts on the problem webpack is generating? I talked about it earlier, didn't you encounter it while updating your binaries? I am pretty sure it's an easy fix, just a matter of preventing it from shortening the global var "modules" or something.

vankxr avatar Feb 07 '20 20:02 vankxr

I am wary of disturbing someone's workflow in the case they are relying on it for one reason or another. I am paranoid that way.

I personally never encountered anything worth keeping VSCode from updating.

I have. When they switched to a new version of Electron.

By the way, any thoughts on the problem webpack is generating? I talked about it earlier, didn't you encounter it while updating your binaries? I am pretty sure it's an easy fix, just a matter of preventing it from shortening the global var "modules" or something.

Not sure what you are referring to. More details? I am sorry I missed any earlier comments.

haneefdm avatar Feb 07 '20 21:02 haneefdm

My loading code does support having multiple sets of binary modules (the version is in the paths so will load the correct one) - so in general when I have been updating them I've kept the current one and the previous version. If they're any further back than that - :shrug:

As for how you were building it Haneef - you don't actually really need modify the serial port packages like it looks like you were doing. You can actually do it all through environment variables:

This is approximately the script I was using for building the various binary modules that were used/planned to be used.

#!/bin/sh

export npm_config_arch=x64
export npm_config_build_from_source=true
export npm_config_disturl=https://atom.io/download/electron
export npm_config_runtime=electron
export npm_config_target=$1
export npm_config_target_arch=x64

HOME=$(pwd)/.electron-gyp-64-$1 npm install serialport usb

You pass in the electron runtime version in as the first parameter.

I actually have updated versions of the Linux and macOS binary modules already prepared - the only reason I haven't finished with the current update and released it is that I am currently lacking a functional Windows box to get the modules for Windows.

Marus avatar Feb 08 '20 01:02 Marus

@Marus I tried doing something like you said, with environment variables, but wasn't able to compile for a specific electron version.

@haneefdm webpack is shortening the global variable modules, so when the code tries to add the path to the binary_modules directory, it actually adds it to the local copy, instead of the global array, and then the module.require call does not work because it finds no serialport since the search path is not there.

vankxr avatar Feb 08 '20 01:02 vankxr

@vankxr - what platform/OS were you trying to do the build on? I know that method still works fine on Linux and macOS (as I just built the 6.1.5 modules right then).

The webpack problem is also interesting - looks like it is rewriting the module.require into using web packs require (instead of native node/electron require). I expect I can come up with a fix - will try that tomorrow.

Marus avatar Feb 08 '20 01:02 Marus

@Marus your script does work. It does link/bind to the specific version of electron. Thanks. I noticed you added 'usb'. Is that a new thing we will need. I don't remember that earlier.

I have a script that also takes the electron version as the argument except I did not have all the npm_config magic so, I used the package.json method. Again, thanks

I have a 64-bit Windows 10 machine but no 32-bit ones. Wonder if I can cross-compile

haneefdm avatar Feb 08 '20 01:02 haneefdm

At least on linux you used to be able to do cross to x86 from an x64 machine - and I believe I did that on Windows as well in earlier versions.

The usb module isn't actually required for current releases; that was in there as I had been experimenting with adding native SWO/ITM output support for the black-magic-probe. But none of that has been released - so at the moment usb could be left off that command.

If you can actually build the windows modules and zip the node_module directories up for me I can try to get the webpack require issue solved and see if we can get an update to this actually out the door - even if it isn't a full solution for the future.

Marus avatar Feb 08 '20 01:02 Marus