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

feat: add conditional load of version #2605

Merged
merged 4 commits into from
Oct 7, 2020
Merged

feat: add conditional load of version #2605

merged 4 commits into from
Oct 7, 2020

Conversation

zekth
Copy link
Member

@zekth zekth commented Oct 7, 2020

Fix: #2603

as discussed in #2604

edit: switching to conditional loading like mentioned in : #2605 (comment)

Checklist

package.json Outdated
"bench": "npm run gen-version && branchcmp -r 2 -g -s \"npm run benchmark\"",
"benchmark": "npm run gen-version && npx concurrently -k -s first \"node ./examples/simple.js\" \"npx autocannon -c 100 -d 5 -p 10 localhost:3000/\"",
"license-checker": "license-checker --production --onlyAllow=\"MIT;ISC;BSD-3-Clause;BSD-2-Clause\"",
"gen-version": "node build/build-version.js"
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't we also add this to prepublish hook?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not aware of the publish stack. Can add if required.

Copy link
Member

Choose a reason for hiding this comment

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

https://github.com/fastify/releasify is usually used.
Since it eventually does npm publish, I think standard npm prepublish hooks should work fine. prepublishOnly seems like the right one.

build/build-version.js Outdated Show resolved Hide resolved
Copy link
Member

@jsumners jsumners left a comment

Choose a reason for hiding this comment

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

I am 👎 on this as well. I don't think we should have to accommodate tools that modify source code they do not own. We ship a package.json with our library. We should be able to work with that package.json at run time. Reading the original issue, it sounds like the tooling the reporter uses removes that package.json and that's not our fault.

@kibertoad
Copy link
Member

@jsumners Original issue seems to be a valid used-case, though. Webpack is a popular solution for packaging serverless functions, and omitting package.json doesn't seem to be wrong in context. Do you think that skipping version check for when package.json is not found would be a better approach?

@jsumners
Copy link
Member

jsumners commented Oct 7, 2020

I think we clearly state that we do not support such scenarios:

### Attention Readers:
> Fastify is not designed to run on serverless environments.
The Fastify framework is designed to make implementing a traditional HTTP/S server easy.
Serverless environments requests differently than a standard HTTP/S server;
thus, we cannot guarantee it will work as expected with Fastify.
Regardless, based on the examples given in this document,
it is possible to use Fastify in a serverless environment.
Again, keep in mind that this is not Fastify's intended use case and
we do not test for such integration scenarios.

@mcollina
Copy link
Member

mcollina commented Oct 7, 2020

I think the best solution is to skip the check if the file could not be loaded.

@zekth
Copy link
Member Author

zekth commented Oct 7, 2020

I think the best solution is to skip the check if the file could not be loaded.

In this case on the non packaged version the test stack will work and the built version would skip it indeed. So here https://github.com/fastify/fastify/blob/master/lib/pluginUtils.js#L89 we would just have to check if this.version is defined.

So if we're unable to load the version we set version to null instead of undefined to know if we tried to load it once?

@mcollina
Copy link
Member

mcollina commented Oct 7, 2020

Il move that check in the module scope of loadVersion() instead., and keep the value of version to undefined.

@zekth zekth changed the title feat: export version in a generated file feat: add conditional load of version Oct 7, 2020
fastify.js Show resolved Hide resolved
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

Would you mind to add a test for this? You might need to use proxyquire to mock fs.

fastify.js Outdated
const pkg = JSON.parse(fs.readFileSync(path.join(__dirname, 'package.json')))
return pkg.version
const pkgPath = path.join(__dirname, 'package.json')
if (fs.existsSync(pkgPath)) {
Copy link
Member

@mcollina mcollina Oct 7, 2020

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Using fs.accessSync requires wrapping the block in a try/catch since that function throws an error on failure rather than returning a boolean like fs.existsSync.

Even addressing that, this change does not resolve the issue. I just tried with this change and the following error is outputted:

fastify not found, proceeding anyway
FastifyError [Error]: fastify-plugin: fastify-compress - expected '3.x' fastify version, '%s' is installed
    at Object.checkVersion (webpack-internal:///./node_modules/fastify/lib/pluginUtils.js:96:63)
    at Object.registerPlugin (webpack-internal:///./node_modules/fastify/lib/pluginUtils.js:110:16)
    at Boot.override (webpack-internal:///./node_modules/fastify/lib/pluginOverride.js:28:57)
    at Plugin.exec (webpack-internal:///./node_modules/avvio/plugin.js:79:33)
    at Boot.loadPlugin (webpack-internal:///./node_modules/avvio/plugin.js:266:10)
    at Task.release (webpack-internal:///./node_modules/fastq/queue.js:140:16)
    at worked (webpack-internal:///./node_modules/fastq/queue.js:182:10)
    at eval (webpack-internal:///./node_modules/avvio/plugin.js:269:7)
    at done (webpack-internal:///./node_modules/avvio/plugin.js:201:5)
    at check (webpack-internal:///./node_modules/avvio/plugin.js:225:9)
    at internal/process/task_queues.js:153:7
    at AsyncResource.runInAsyncScope (async_hooks.js:186:9)
    at AsyncResource.runMicrotask (internal/process/task_queues.js:150:8)
    at processTicksAndRejections (internal/process/task_queues.js:97:5) {
  code: 'FST_ERR_PLUGIN_VERSION_MISMATCH',
  statusCode: 500
}

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

Copy link
Member

@jsumners jsumners left a comment

Choose a reason for hiding this comment

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

LGTM

@mcollina mcollina merged commit f22cb2f into fastify:master Oct 7, 2020
@zekth zekth deleted the gen_version branch October 7, 2020 15:49
@zekth zekth mentioned this pull request Oct 7, 2020
4 tasks
@github-actions
Copy link

github-actions bot commented Feb 6, 2022

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 6, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Plugin registration no longer works with webpack
6 participants