gaelj / BlazorCodeMirror6

Blazor wrapper for CodeMirror 6
MIT License
20 stars 2 forks source link

Compile error in Rollup #22

Closed pm64 closed 10 months ago

pm64 commented 10 months ago

I've run into an issue compiling the current build that is related to the following target:

   <Target Name="Rollup" BeforeTargets="BeforeBuild;BeforeRebuild">
    <Exec Command="cd NodeLib &amp;&amp; npx rollup --config" />
  </Target>

When "npx rollup --config" is executed in the NodeLib directory, the following error is triggered:

[!] Error: Cannot find module './constants'
Require stack:
- D:\MyProject\NodeLib\node_modules\picomatch\lib\utils.js
- D:\MyProject\NodeLib\node_modules\picomatch\lib\scan.js
- D:\MyProject\NodeLib\node_modules\picomatch\lib\picomatch.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1075:15)
    at Function.Module._load (node:internal/modules/cjs/loader:920:27)
    at Module.require (node:internal/modules/cjs/loader:1141:19)
    at require (node:internal/modules/cjs/helpers:110:18)
    at Object.<anonymous> (D:\MyProject\NodeLib\node_modules\picomatch\lib\utils.js:10:5)
    at Module._compile (node:internal/modules/cjs/loader:1254:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
    at Module.load (node:internal/modules/cjs/loader:1117:32)
    at Function.Module._load (node:internal/modules/cjs/loader:958:12)
    at Module.require (node:internal/modules/cjs/loader:1141:19)

Did something change about the project structure that could account for this error? Or perhaps I've overlooked something? Let me know if I can provide further info.

gaelj commented 10 months ago

I'm building on Linux and it seems the commands I use don't work the same in windows. I think I have the workaround but I won't have time to implement it straight away (hopefully this evening)

If you're in a hurry you can try yourself based on this chat gpt info:

(Or just delete those commands from the csproj and run them manually, replacing && with distinct commands)


To make your MSBuild targets compatible with Windows, you'll need to address the quirks of Windows command processing. Here are some steps you can follow:

Modifying the .csproj File

  1. Use Full Command Paths: Specify the full path to the npm and rollup commands. This avoids any issues with the current directory.

  2. Separate Script Files: Create separate script files for Windows (batch files) and Linux (shell scripts). Call these scripts from your MSBuild targets. This allows you to handle OS-specific differences in your scripts.

  3. Conditional MSBuild Targets: Use conditions in your MSBuild targets to execute different commands based on the operating system. Here's a simplified example:

    <Target Name="NpmInit" BeforeTargets="BeforeBuild;BeforeRebuild;Rollup">
     <Exec Command="scripts/npm_install.sh" Condition="'$(OS)' != 'Windows_NT'" />
     <Exec Command="scripts\\npm_install.bat" Condition="'$(OS)' == 'Windows_NT'" />
    </Target>
    
    <Target Name="Rollup" BeforeTargets="BeforeBuild;BeforeRebuild">
     <Exec Command="scripts/rollup.sh" Condition="'$(OS)' != 'Windows_NT'" />
     <Exec Command="scripts\\rollup.bat" Condition="'$(OS)' == 'Windows_NT'" />
    </Target>
    • npm_install.sh and rollup.sh for Linux.
    • npm_install.bat and rollup.bat for Windows.

Creating Script Files

  1. Linux Scripts (.sh): These will contain your original commands.

    Example (npm_install.sh):

    #!/bin/bash
    cd NodeLib && npm install

    Example (rollup.sh):

    #!/bin/bash
    cd NodeLib && npx rollup --config
  2. Windows Scripts (.bat): Windows batch script with modified commands.

    Example (npm_install.bat):

    cd NodeLib
    npm install
    cd ..

    Example (rollup.bat):

    cd NodeLib
    npx rollup --config
    cd ..

Testing

After setting this up, test on both Windows and Linux systems to ensure everything works as expected.

Summary

This approach allows you to maintain a single codebase that's compatible with both Windows and Linux, and it keeps the OS-specific quirks neatly tucked away in their respective script files.

gaelj commented 10 months ago

Here's the link to the full conversation

https://chat.openai.com/share/4a9044d5-32d5-486f-a585-b70d97ac6346

pm64 commented 10 months ago

Hi @gaelj, thanks for the reply.

An initial reading of this info suggests that it will not resolve the issue, because the .bat file executed on Windows will still run the command "npx rollup --config" in the NodeLib directory, triggering the error. So I don't think there's any issue with your .csproj.

To test this, I'm simply running this npx command directly from Terminal, actually bypassing the .csproj file completely.

So, I don't think there's any Windows incompatibility in your .csproj as suggested by ChatGPT. I'm able to build your previous commit, which has the same .csproj targets.

Rather, it seems to be related to the Node modules or dependencies, perhaps a problem that is specific to my system..

gaelj commented 10 months ago

You need to run the commands in the CodeMirror6/NodeLib directory. You can delete the NodeLib directory that was creates in your project root.

pm64 commented 10 months ago

Hey @gaelj, CodeMirror6/NodeLib is precisely the directory in which I'm running the commands. No NodeLib directory has been created in my project root.

gaelj commented 10 months ago

Your error stack mentions

D:\MyProject\NodeLib

This is the problem. Anyway don't worry I'll fix it ASAP (I have a windows VM)

pm64 commented 10 months ago

My aplogies @gaelj, that's just an error in the composition of my message. I edited the error stack to shorten my project's path length for readability, and in doing so, messed up the path.

The NodeLib path referenced in the error stack should ACTUALLY read D:\MyProject\CodeMirror6\NodeLib.

Here's the command and full corrected stack trace:

PS D:\MyProject\CodeMirror6\NodeLib> npx rollup --config
[!] Error: Cannot find module './constants'
Require stack:
- D:\MyProject\CodeMirror6\NodeLib\node_modules\picomatch\lib\utils.js
- D:\MyProject\CodeMirror6\NodeLib\node_modules\picomatch\lib\scan.js
- D:\MyProject\CodeMirror6\NodeLib\node_modules\picomatch\lib\picomatch.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1075:15)
    at Function.Module._load (node:internal/modules/cjs/loader:920:27)
    at Module.require (node:internal/modules/cjs/loader:1141:19)
    at require (node:internal/modules/cjs/helpers:110:18)
    at Object.<anonymous> (D:\MyProject\CodeMirror6\NodeLib\node_modules\picomatch\lib\utils.js:10:5)
    at Module._compile (node:internal/modules/cjs/loader:1254:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
    at Module.load (node:internal/modules/cjs/loader:1117:32)
    at Function.Module._load (node:internal/modules/cjs/loader:958:12)
    at Module.require (node:internal/modules/cjs/loader:1141:19)

PS - your ChatGPT's personality is hilarious .. "Ah, classic Node.js module resolution hell" LMAO

But I think its answer to your 2nd question and everything afterward is either not relevant in this case or hallucination. I think your .csproj is fine. But let's see what experience you have with your Win VM.

Anything I can do to help, just say the word.

pm64 commented 10 months ago

SOLVED.

As I suspected, this was an issue specific to my system.

I still encourage you to test the build in Windows, but currently I'm not aware of any issues building for that OS.

To resolve this, I simply deleted NodeLib/node_modules/* and re-ran npm install -- problem magically fixed.

Following this, rollup ran successfully and the project built beautifully.

Thanks for your guidance, sorry to have wasted your time.