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

fix(dev): bind plugin context functions #14569

Merged
merged 7 commits into from
Nov 28, 2023
Merged

Conversation

patricklx
Copy link
Contributor

@patricklx patricklx commented Oct 10, 2023

rollup binds the plugin context functions to itself and plugins use that.
e.g. commonjs.

closes rollup/plugins#1603


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the PR Title Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@sapphi-red sapphi-red added rollup plugin compat p3-minor-bug An edge case that only affects very specific usage (priority) labels Oct 10, 2023
@sapphi-red
Copy link
Member

Would you add a test to https://github.com/vitejs/vite/blob/main/packages/vite/src/node/server/__tests__/pluginContainer.spec.ts?

@patricklx patricklx force-pushed the patch-1 branch 2 times, most recently from b4f4b1f to e5023da Compare October 10, 2023 11:13
@patricklx
Copy link
Contributor Author

@sapphi-red this is ready

sapphi-red
sapphi-red previously approved these changes Oct 11, 2023
Copy link
Member

@sapphi-red sapphi-red left a comment

Choose a reason for hiding this comment

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

Thanks!

Copy link
Member

@bluwy bluwy left a comment

Choose a reason for hiding this comment

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

I guess there's no harm to bind this, but Rollup doesn't call bind(this) on the plugin context either: https://github.com/rollup/rollup/blob/fac5f1c1c12dc0409ff090664e305586747dc2f7/src/utils/PluginContext.ts#L60-L114

It happens that the commonjs plugin assumes that this.load isn't binding sensitive and it worked. So having this fixed on the commonjs plugin side would also be nice.

@patricklx
Copy link
Contributor Author

I looked at the functions again and i noticed that the concept is to have all functions either with bind or not binding sensitive.

@lukastaegert
Copy link

but Rollup doesn't call bind(this) on the plugin context either

Of course it does not bind. You do not need to bind if you do not access this.

@lukastaegert
Copy link

Ok fair enough, this.addWatchFile indeed breaks that assumption...

@bluwy
Copy link
Member

bluwy commented Oct 27, 2023

@lukastaegert I don't mind to proceed with this PR if you intended for the plugin context to already be bound. Would you prefer we go with this then?

I was thinking it's odd that you'd pass this.load around and expect the this context to still maintain. Usually handling references like this need to be coded defensively with a this.load.bind(this). And we only found about this issue after years.

If we do go with this PR, it's not completed yet. There's still functions not bound like this.getModuleIds, this.getModuleInfo, this.emitFile etc (basically all just in case we ever use this). After that we could merge it.

@lukastaegert
Copy link

By the way, the this reference in this.addWatchFile has been removed now.

patak-dev
patak-dev previously approved these changes Nov 27, 2023
Copy link
Member

@patak-dev patak-dev left a comment

Choose a reason for hiding this comment

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

I would prefer these functions to not be bound, but given that this was already an expectation in the Rollup ecosystem, I think we could merge this PR. Later on, we could still decide to remove this feature, but do it in sync both in Rollup and Vite if there is consensus and it is justified.

@lukastaegert
Copy link

but do it in sync both in Rollup

Well, Rollup does not bind, it just does not use this 😉

@patak-dev
Copy link
Member

patak-dev commented Nov 27, 2023

but do it in sync both in Rollup

Well, Rollup does not bind, it just does not use this 😉

Ha, yes, we are on the same page, but just in case for reference I mean if it is ever decided that users can't expect context functions to work without being bound to its context (no matter how it is implemented), it would be good if it is a change in both Rollup and Vite dev.

By the way, all methods in the Vite dev server can also be used without being bound to the vite server, but this is an undocumented implementation detail. I hope no one is relying on this right now, maybe we'll end up having issues in the future if we ever change to a different implementation.

@patak-dev patak-dev merged commit cb3243c into vitejs:main Nov 28, 2023
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p3-minor-bug An edge case that only affects very specific usage (priority) rollup plugin compat
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants