actboy168 / vscode-tasks

MIT License
37 stars 14 forks source link

npm tasks sometimes doesn't respect path #45

Closed henrikxberg closed 1 year ago

henrikxberg commented 1 year ago

This is a bit of a strange problem that I only suspect is caused by this plugin. I have tasks of type npm with the path option to make npm execute a script from a package.json in a subfolder and sometimes i start vscode and the task button tries to execute the script in the root folder for some reason. Then I restart vscode and it suddenly works again, sometimes it takes another restart. I haven't found a pattern so I don't know if it depends on anything I'm doing or just random. But the built in task execution always run the correct script.

example task: { "label": "backend", "type": "npm", "script": "start", "path": "server" }

Any ideas what could be causing it? Thanks for a great plugin. /henrik

actboy168 commented 1 year ago

Do you have other tasks? Maybe other tasks are actually executed?

henrikxberg commented 1 year ago

No there are only a few tasks defined. And none for executing root folder scripts. But I did see some duplicate names when executing through the menu so maybe vscode does something wrong when using path. I can't check right now but will get back to you.

ArcanoxDragon commented 1 year ago

I am also having this problem. I have two workspace folders. The following configuration is what works correctly:

Again, this configuration works. I made a change to this configuration, however, and encountered what seems to be the same problem as the OP. The change I made was to add a new NPM script to the package.json file in the client folder named serve. I then renamed the Build (Watch) task in the client folder to Build (Serve/Watch), and I changed the script that it runs to serve instead of watch.

After making that change, the Build (Serve/Watch) task still works, but the Preconstruct Watch task in the external folder has suddenly started running the watch script in the client folder. The other three tasks work fine. I'm perplexed as to why the task in the external folder broke when the only task I changed is the one in the client folder...

henrikxberg commented 1 year ago

While it might well be the same root cause. I have only one workspace and one tasks.json in the root .vscode. but executing package.json scripts defined in subfolders.

henrikxberg commented 1 year ago

Ok it has something to do with the fact that there is a script in the root package.json with the same name as the script the task is trying to run in a subfolder. maybe the task button just looks for a task with the right script and runs it?

henrikxberg commented 1 year ago

here is a simple project demonstrating the issue. test1 should echo "1" but it echoes "root". test2 does what it is supposed to do for some reason even though it essentially doing the same thing.

actboy168 commented 1 year ago

here is a simple project demonstrating the issue. test1 should echo "1" but it echoes "root". test2 does what it is supposed to do for some reason even though it essentially doing the same thing.

I can't reproduce it.

ArcanoxDragon commented 1 year ago

Could it be specific to a particular OS or package manager? I'm using Yarn on Windows 10, and have VS Code set up to use Yarn for running NPM scripts.

actboy168 commented 1 year ago

Could it be specific to a particular OS or package manager? I'm using Yarn on Windows 10, and have VS Code set up to use Yarn for running NPM scripts.

I use windows 10 and yarn 3.6 to test.

henrikxberg commented 1 year ago

I see the same issue in both Windows 10 and 11. Even when trying setting vscode to use yarn. I have disabled all other plugins. What else could cause it?

When clicking the test1 button:

Executing task: yarn run test

yarn run v1.22.19 warning package.json: No license field $ echo root root Done in 0.22s.

henrikxberg commented 1 year ago

Ah I think I found it. In matchTask you only compare for every property in task.k but path isn't there when not defined so test1 is matched with the script vscode finds in package.json. Maybe you cannot replicate it because your vscode doesn't automatically include everything from package.json among the tasks? I don't know why mine does. just guessing here.

actboy168 commented 1 year ago

@henrikxberg However, the executed task is always a task provided by VSCode. But none of the task's path is root. The extension may execute a wrong task, but it will never execute a non-existent task.

henrikxberg commented 1 year ago

For the test project I created matchTask matches against the following tasks:

{type: 'npm', script: 'test'} {type: 'npm', script: 'install'} {type: 'npm', script: 'test', path: 'test1'} {type: 'npm', script: 'install', path: 'test1'} {type: 'npm', script: 'test', path: 'test2'} {type: 'npm', script: 'install', path: 'test2'}

So test1 matches against the first one since no path check is ever done. Maybe you could match against all tasks and use the one among the matching that has most props.

actboy168 commented 1 year ago

For the test project I created matchTask matches against the following tasks:

{type: 'npm', script: 'test'} {type: 'npm', script: 'install'} {type: 'npm', script: 'test', path: 'test1'} {type: 'npm', script: 'install', path: 'test1'} {type: 'npm', script: 'test', path: 'test2'} {type: 'npm', script: 'install', path: 'test2'}

So test1 matches against the first one since no path check is ever done. Maybe you could match against all tasks and use the one among the matching that has most props.

Looks like some configuration of VSCode will add more tasks.