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

Capture new optimized deps #2135

Merged
merged 8 commits into from
Oct 4, 2024
Merged

Capture new optimized deps #2135

merged 8 commits into from
Oct 4, 2024

Conversation

ef4
Copy link
Contributor

@ef4 ef4 commented Oct 2, 2024

Builds on the tests and findings in #1876, with a retry-based implementation.

The strategy here is that a newly added dep is a pretty rare occurrence, so it's better to let most resolutions run normally and only check them after the fact to see if they could have been new un-optimized dependencies, then do a second resolve for them that's adjusted so to conform to vite's rules about what is allowed to be optimized, as explained by @patricklx in #1876.

We really only need to hide any node_modules in the fromFile. The rest (like always having a non-relative specifier) is already handled in core module-resolver.

@ef4 ef4 added the bug Something isn't working label Oct 2, 2024

debug('maybeCaptureNewOptimizedDep: doing re-resolve for %s ', foundFile);

let jumpRoot = join(tmpdir, 'embroider-vite-jump', hashed(pkg.root));
Copy link
Contributor

@patricklx patricklx Oct 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, nice trick
There was an advantage in the previous implementation that it was doing it only for rewritten packages, so auto-upgraded.

It might be that now this will also optimise other deps?

If a dep is excluded in vite, the default behaviour is to also exclude the dependencies of it. Because its in node_modules.
If this really does optimise deps of excluded deps i think it would be nice and definitely a candidate for another vite plugin :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was an advantage in the previous implementation that it was doing it only for rewritten packages, so auto-upgraded.

Yeah, I think that probably left a bug for native v2 addons, since they also need this behavior but won't get rewritten. Consider your app depends on addon-one which depends on addon-two and both are native v2 addons so neither is rewritten.

During dev if you add a new usage of a component from addon-two, module resolving will look like:

  1. defaultResolve runs for your-app/components/the-new-one and doesn't find it.
  2. fallbackResolve finds it in the mergeMap and retries with request.alias('addon-two/_app_/components/the-new-one.js').rehome('/path/to/your-app/node_modules/addon-one/package.json'). The rehome here is chosen to be in a package that can definitely resolve addon-two.
  3. defaultResolve finds the file, and the specifier is a bare import, but the fromFile includes node_modules, so you get an un-optimized dependency.

If a dep is excluded in vite, the default behaviour is to also exclude the dependencies of it. Because it's in node_modules.

That's a good point, and another reason why vite's isInNodeModules check is silly. It should really be crawling the package graph to decide where the boundary between not-optimized and optimized packages is.

Yeah, a plugin could fix that problem for everyone, although IMO it would be better to fix it inside vite. I think their package info caches are equivalently powerful to the packageCache in embroider, so could solve the problem without a lot of extra infrastructure.

@ef4
Copy link
Contributor Author

ef4 commented Oct 2, 2024

@patricklx I don't understand yet why the new tests are timing out on windows. It looks like we're failing to shutdown the server between tests, since the next run logs that it had to try multiple ports before finding a free one.

@patricklx
Copy link
Contributor

@patricklx I don't understand yet why the new tests are timing out on windows. It looks like we're failing to shutdown the server between tests, since the next run logs that it had to try multiple ports before finding a free one.

Yeah, I also saw that. It has been working before and i didn't change the tests. Maybe vite changed something?

@ef4
Copy link
Contributor Author

ef4 commented Oct 3, 2024

Running locally on windows I think the problem is that there's a module cycle in the tests, and that cycle apparently evaluates in a different order on windows.

The cycle is that the service in my-services-addon imports app/app.js, but app/app.js imports the core entrypoint which imports the service.

I think if we change the test to use a different example file in app rather than app.js it will address the cycle.

@ef4
Copy link
Contributor Author

ef4 commented Oct 4, 2024

The cycle problem was real and I could reliably reproduce it and it's fixed.

I've seen the windows tests passing locally. I'm also seeing inconsistent behaviors. It looks like sometimes the prebuild hasn't really finished successfully, because you get failures during dep optimization that can only be explained by missing prebuild output.

On CI it's so slow I've never seen it finish. So I'm skipping them on windows.

@ef4 ef4 merged commit 72ee15d into main Oct 4, 2024
153 of 154 checks passed
@ef4 ef4 deleted the fix-vite-dep-optimizer2 branch October 4, 2024 03:41
@github-actions github-actions bot mentioned this pull request Oct 4, 2024
@patricklx
Copy link
Contributor

The cycle problem was real and I could reliably reproduce it and it's fixed.

I've seen the windows tests passing locally. I'm also seeing inconsistent behaviors. It looks like sometimes the prebuild hasn't really finished successfully, because you get failures during dep optimization that can only be explained by missing prebuild output.

On CI it's so slow I've never seen it finish. So I'm skipping them on windows.

Yeah, i saw it pass sometimes, took like 16min...
Great to see this fixed finally 👍🚀🚀

@patricklx patricklx mentioned this pull request Oct 4, 2024
6 tasks
@ef4
Copy link
Contributor Author

ef4 commented Oct 4, 2024

@patricklx thanks for your work on this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants