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

@uppy/companion: merge config before validating #4827

Closed

Conversation

andyroyle
Copy link

Running validateConfig can throw errors and stop the application from starting. If you're using environment variables to pass configuration then these need to be merged in before attempting to validate the configuration.

A concrete example of why this is needed is if you're setting COMPANION_UPLOAD_URLS when running NODE_ENV=production the application will fail to start because validateConfig requires that uploadUrls is specified.

@mifi
Copy link
Contributor

mifi commented Jan 6, 2024

If you're using environment variables to pass configuration then these need to be merged in before attempting to validate the configuration

in this PR you call validateConfig after merging defaultOptions with optionsArg. Prior to this PR, we call validateConfig only on optionsArg. it's not obvious to me why this change will make a difference because afaik COMPANION_UPLOAD_URLS (uploadUrls) gets validated the same way in both cases. defaultOptions doesn't even contain uploadUrls

@andyroyle
Copy link
Author

it's not obvious to me why this change will make a difference because afaik COMPANION_UPLOAD_URLS (uploadUrls) gets validated the same way in both cases

When running with NODE_ENV=production, an empty uploadUrls config option prevents the service from starting (the validateConfig method throws an error). If you are setting uploadUrls using the environment variables and these get merged in after calling validateConfig, then uploadUrls will always be empty and validateConfig will throw an error.

@mifi
Copy link
Contributor

mifi commented Jan 31, 2024

still not sure i understand. if I run companion on main with NODE_ENV=production, it crashes as expected:

NODE_ENV=production yarn dev:with-companion
[nodemon] 2.0.19
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): packages/@uppy/companion/src/**/*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node -r dotenv/config ./packages/@uppy/companion/src/standalone/start-server.js`
uppy/packages/@uppy/companion/src/config/companion.js:101
    if (process.env.NODE_ENV === 'production') throw new Error('uploadUrls is required')
                                               ^

Error: uploadUrls is required

If I run it with the env variable COMPANION_UPLOAD_URLS it seems to start correctly:

NODE_ENV=production COMPANION_UPLOAD_URLS=http://google.com yarn dev:with-companion
[nodemon] 2.0.19
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): packages/@uppy/companion/src/**/*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node -r dotenv/config ./packages/@uppy/companion/src/standalone/start-server.js`
companion: 2024-01-31T08:51:16.502Z [info] jobs.cleanup.start starting clean up job
companion: 2024-01-31T08:51:16.523Z [info]  Welcome to Companion! v4.12.0
companion: 2024-01-31T08:51:16.523Z [info]  Listening on http://localhost:3020

If you are setting uploadUrls using the environment variables and these get merged in after calling validateConfig

env variables are merged in before module.exports.app is even called. it's merged in getCompanionOptions

const companionOptions = getCompanionOptions(inputCompanionOptions)
exports.getCompanionOptions = (options = {}) => {

@mifi
Copy link
Contributor

mifi commented Feb 5, 2024

as I don't understand the benefit of this change and it only introduces a risk of bugs when changing the order, i'm going to close this for now

@mifi mifi closed this Feb 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants