question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug] Creating virtual files on the fly causes infinite watched rebuilds

See original GitHub issue
  • I’d be willing to submit the fix

Describe the bug

I don’t necessarily know all the virtual files I want to create up front, so I’ve been creating them on a beforeResolve hook if they match a pattern. This worked fine up to and including 0.3.1. Since 0.3.2 this causes infinite rebuilds (which I guess makes sense given I’m creating files mid way through a build).

I’m not sure if this was ever a supported scenario so this may well be a feature request, but it was working until very recently

Is there a way to keep this working? Or alternatively a better approach?

To Reproduce

Add the following plugin to your webpack config and import testfile.virtual.js from a file in the build. Start a watched build.

class ReproPlugin {
    apply(compiler) {
        const virtualModules = new VirtualModulesPlugin();
        virtualModules.apply(compiler);

        compiler.hooks.compilation.tap(this.constructor.name, (compilation, { normalModuleFactory }) => {
            normalModuleFactory.hooks.beforeResolve.tap(this.constructor.name, (result) => {
                if (/\.virtual\.js$/i.test(result.request)) {
                    virtualModules.writeModule(
                        path.resolve(result.context, result.request),
                        '// test content');
                }
                // uncomment if webpack < 5
                // return result;
            });
        });
    }
}

Prior to 0.3.2 this would build happily and wait for new changes. In 0.3.2 it builds repeatedly.

Screenshots

If applicable, add screenshots to help explain your problem.

Environment if relevant (please complete the following information):

  • OS: Windows 10
  • Node version: 12.16.3
  • Webpack version: 5.3.2
  • webpack-virtual-modules: 0.3.2

Additional context

The virtual files I’m creating don’t exist in any form on disk so I wasn’t able get a custom loader to work instead.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
andyroogercommented, Nov 1, 2020

@larixer You’re 100% right. I had assumed we didn’t need to resolve the same path more than once in a compilation.

This works absolutely fine with latest webpack and webpack-virtual-modules

class ReproPlugin {
    apply(compiler) {
        const virtualModules = new VirtualModulesPlugin();
        virtualModules.apply(compiler);

        const virtualized = new Set();

        compiler.hooks.compilation.tap(this.constructor.name, (compilation, { normalModuleFactory }) => {
            normalModuleFactory.hooks.beforeResolve.tap(this.constructor.name, (result) => {
                if (/\.virtual\.js$/i.test(result.request)) {
                    const fullPath = path.resolve(result.context, result.request);
                    if(!virtualized.has(fullPath)) {
                        virtualized.add(fullPath);
                        virtualModules.writeModule(fullPath, '// test content');
                    }
                }
                // uncomment if webpack < 5
                // return result;
            });
        });
    }
}

So no feature request needed and apologies for the spurious bug. Thank you, I appreciate the help.

0reactions
andyroogercommented, Nov 18, 2020

@StephanGerbeth can do.

It’s worth trying to populate the files early (in the virtual modules constructor or compilation hook) like in the readme examples and make sure the issue is not to do with how that virtual modules interacts with that plugin in general.

If not I’m happy to look through a sandbox, though I don’t think I’m using the code above in conjunction with other loaders anywhere, so no guarantees I’ll be able to make it work either 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Virtual files are always rebuilt in webpack watch mode #5824
I want to report a bug. What is the current behavior? When using a input file system decorator that serves files in memory,...
Read more >
Bug listing with status RESOLVED with resolution FIXED as at ...
configure " status:RESOLVED resolution:FIXED severity:normal · Bug:188 - "environment variable GZIP cause pilot-link-0.9.6.ebuild (kde) to fail ?
Read more >
How to Fix Video Lag, Stutter and Glitches issues?
Video lag, stutter and glitches are very common issues. But do you know the ways to fix them. Read out the blow to...
Read more >
How to fix a Windows 10 boot loop | TechTarget
If a Windows desktop is stuck in an infinite reboot loop, as an IT professional, you must understand why it is happening and...
Read more >
Getting Windows 11 Boot Loop? Troubleshoot This Error Now
Windows 11 boot loop error is mainly caused due to faulty registry files that can prevent PCs from starting normally. This creates infinite...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found