Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could not find source file when running in watch mode with rollup-plugin-vue #271

Closed
danielvy opened this issue Jun 14, 2021 · 5 comments · Fixed by #364
Closed

Could not find source file when running in watch mode with rollup-plugin-vue #271

danielvy opened this issue Jun 14, 2021 · 5 comments · Fixed by #364
Assignees
Labels
kind: regression Specific type of bug -- past behavior that worked is now broken problem: removed issue template OP removed the issue template without good cause scope: integration Related to an integration, not necessarily to core (but could influence core) scope: vue Related to integration with Vue (rollup-plugin-vue is long archived), not core scope: watch mode Related to Rollup's watch mode solution: workaround available There is a workaround available for this issue

Comments

@danielvy
Copy link

What happens and why it is wrong

When running rollup in watch mode, the project will initially compile but changes to some files that trigger rollup watch, will cause a source file not found error from rp2:

[!] (plugin rpt2) Error: Could not find source file: '/<path removed>/watch-err/source/lib/module1.ts'.
Error: Could not find source file: '/<path removed>/watch-err/source/lib/module1.ts'.
    at getValidSourceFile (/<path removed>/watch-err/node_modules/typescript/lib/typescript.js:153646:29)
    at Object.getSyntacticDiagnostics (/<path removed>/watch-err/node_modules/typescript/lib/typescript.js:153896:52)
    at /<path removed>/watch-err/node_modules/rollup-plugin-typescript2/src/index.ts:306:23
    at TsCache.getDiagnostics (/<path removed>/watch-err/node_modules/rollup-plugin-typescript2/src/tscache.ts:298:49)
    at TsCache.getSyntacticDiagnostics (/<path removed>/watch-err/node_modules/rollup-plugin-typescript2/src/tscache.ts:238:15)
    at /<path removed>/watch-err/node_modules/rollup-plugin-typescript2/src/index.ts:304:15
    at /<path removed>/watch-err/node_modules/rollup-plugin-typescript2/src/tscache.ts:183:61
    at arrayEach (/<path removed>/watch-err/node_modules/rollup-plugin-typescript2/node_modules/lodash/lodash.js:516:11)
    at Function._.each [as forEach] (/<path removed>/watch-err/node_modules/rollup-plugin-typescript2/node_modules/lodash/lodash.js:9368:14)
    at TsCache.walkTree (/<path removed>/watch-err/node_modules/rollup-plugin-typescript2/src/tscache.ts:183:4)

Repository with reproduction code

Steps to reproduce:

  1. Run rollup -c rollup.config.js --watch
  2. Trigger rollup watch by re-saving module2.ts or index.ts. Note that triggering watch with module1.ts will not produce the error.

I assume this is related to the directory structure of the project which uses baseUrl to point up to the "lib" dir. Also, I was not able to reproduce this without Vue.

Environment

MacOS 10.15
Node 16

@agilgur5 agilgur5 changed the title Source file not found error when running in "watch" mode Could not find source file when running in watch mode with rollup-plugin-vue May 2, 2022
@agilgur5 agilgur5 added the scope: integration Related to an integration, not necessarily to core (but could influence core) label May 2, 2022
@agilgur5 agilgur5 added scope: cache Related to the cache problem: removed issue template OP removed the issue template without good cause solution: workaround available There is a workaround available for this issue labels Jun 9, 2022
@agilgur5
Copy link
Collaborator

agilgur5 commented Jun 9, 2022

another workaround

Per the log, this seems to be an issue in the cache logic (only with Vue for whatever reason):

    at TsCache.walkTree (/<path removed>/watch-err/node_modules/rollup-plugin-typescript2/src/tscache.ts:183:4)

So, as such, one can also set clean: true as a workaround to skip the cache entirely.
EDIT: I tried that and it didn't fix anything. I'm also way more familiar with the cache code now (see my many recent cache PRs), and walkTree is called even when there is no cache, as it's used to walk the dependency tree, which exists regardless if there is a cache or not.

walkTree is only called in watch mode after the first run, that being said, in order to re-type-check any files in the chain of the file that changed. It's also only called when check: true, so turning off type-checking, i.e. setting check: false is another workaround.

bit of investigation

When searching the error at hand, you'll get a lot of results from other TS integrations, such as ts-jest. Interestingly enough, ts-jest hit into this same error when they introduced a cache: kulshekhar/ts-jest#1506 .

From that issue, it's also not clear that the few PRs around that have entirely fixed the bug. It's likely a race condition (similar to #243), so they've probably solved several races but not necessarily all of them.
Some of those might be usable within rpt2 too, but we have different caching mechanisms and very different code around it. I've looked at the cache code a bit and couldn't pinpoint why this issue occurs, but if it is indeed a race condition, then it's due to an anomaly, so something unexpected is happening that the code doesn't handle (vs., in general, code is designed to handle an expected solution. i.e. the expected code looks fine, but some unexpected scenario may be missing).

Also, some comments suggest that this is due to cyclic dependencies (which may relate to the Vue plugin), and indeed the cache code runs a different graph algorithm depending if the module graph is acyclic or not.

A verbosity: 3 log was not provided here as the issue template asks, so can't tell if it's due to that. The verbosity: 3 log would output detailed information about the whole module graph, so looking at could be quite helpful for debugging.

EDIT: ^that all seems to be unrelated 😕 . was chasing a red herring, my bad

@agilgur5 agilgur5 added the scope: watch mode Related to Rollup's watch mode label Jun 9, 2022
@agilgur5
Copy link
Collaborator

agilgur5 commented Jun 9, 2022

Hmm... looking at some older watch mode issues (see the new label) it seems that was supposed to have been fixed by #32. And that it's popped up a few times.

That and #95 also suggest the issue might be within the LanguageServiceHost implementation, and unfortunately, that is the most "gotcha" sparsely documented thing in the TS Compiler API (see also #351, #234 (comment)), so it's often pretty difficult to find out what's going on there without straight up debugging TS's own source code.

@agilgur5 agilgur5 added kind: bug Something isn't working properly kind: regression Specific type of bug -- past behavior that worked is now broken and removed scope: cache Related to the cache kind: bug Something isn't working properly labels Jun 22, 2022
@agilgur5
Copy link
Collaborator

agilgur5 commented Jun 22, 2022

Ah I've traced the bug and figured it out! Turns out this is a regression that was actually caused by the fix for #95.

This line was added to setSnapshot but not to getScriptSnapshot.
getScriptSnapshot is only called in the callback for walkTree, so that explains why this wasn't found until now.

I'll write up a PR for this in a jiffy! EDIT: see #364
Thanks for the repro @danielvy! I was able to change this code locally in node_modules of your repro and then the error went away, so can confirm the fix works 🙂

@danielvy
Copy link
Author

danielvy commented Jun 25, 2022

@agilgur5

Great news. Thank you for taking the time to fix this. I can go back to using rollup's watch instead of relying on nodemon during development.

@agilgur5
Copy link
Collaborator

#364 has been released in 0.33.0

@agilgur5 agilgur5 added the scope: vue Related to integration with Vue (rollup-plugin-vue is long archived), not core label Mar 14, 2023
Repository owner locked as resolved and limited conversation to collaborators Mar 14, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
kind: regression Specific type of bug -- past behavior that worked is now broken problem: removed issue template OP removed the issue template without good cause scope: integration Related to an integration, not necessarily to core (but could influence core) scope: vue Related to integration with Vue (rollup-plugin-vue is long archived), not core scope: watch mode Related to Rollup's watch mode solution: workaround available There is a workaround available for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants