-
Notifications
You must be signed in to change notification settings - Fork 748
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 support for redirecting Wrangler to a generated config when running deploy commands #7442
base: main
Are you sure you want to change the base?
Conversation
🦋 Changeset detectedLatest commit: 1f9c943 The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
5a91f5b
to
b3bf041
Compare
A wrangler prerelease is available for testing. You can install this latest build in your project with: npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12480724262/npm-package-wrangler-7442 You can reference the automatically updated head of this PR with: npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/prs/7442/npm-package-wrangler-7442 Or you can use npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12480724262/npm-package-wrangler-7442 dev path/to/script.js Additional artifacts:wget https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12480724262/npm-package-cloudflare-workers-bindings-extension-7442 -O ./cloudflare-workers-bindings-extension.0.0.0-v356c10f90.vsix && code --install-extension ./cloudflare-workers-bindings-extension.0.0.0-v356c10f90.vsix npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12480724262/npm-package-create-cloudflare-7442 --no-auto-update npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12480724262/npm-package-cloudflare-kv-asset-handler-7442 npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12480724262/npm-package-miniflare-7442 npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12480724262/npm-package-cloudflare-pages-shared-7442 npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12480724262/npm-package-cloudflare-unenv-preset-7442 npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12480724262/npm-package-cloudflare-vitest-pool-workers-7442 npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12480724262/npm-package-cloudflare-workers-editor-shared-7442 npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12480724262/npm-package-cloudflare-workers-shared-7442 npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12480724262/npm-package-cloudflare-workflows-shared-7442 Note that these links will no longer work once the GitHub Actions artifact expires.
Please ensure constraints are pinned, and |
e251387
to
3756ab2
Compare
0e8fc8a
to
a54d88c
Compare
48b73e0
to
a06374a
Compare
e7fd32d
to
f33b32a
Compare
fixtures/import-npm/package.json
Outdated
"check:type": "npm run _clean_install && npm run check:type --workspaces", | ||
"test:ci": "npm run _clean_install && npm run test:ci --workspaces", | ||
"test:watch": "npm run _clean_install && npm run test:watch --workspaces", | ||
"type:tests": "npm run _clean_install && npm run type:tests --workspaces" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found that this fixture's tests kept failing; and I think it was because we need to ensure that the node_modules directory was in fact created by the npm i
command here, and not a pnpm i
command that is naturally run when setting up the workers-sdk project.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Furthemore I couldn't just run _clean_install in front of each of these scripts because turbo tries to run then in parallel. So the solution was to add a turbo dependency instead, which means that this only runs once.
@@ -99,27 +99,25 @@ describe("unstable_dev()", () => { | |||
const childWorker = await unstable_dev( | |||
"${child.replaceAll("\\", "/")}/src/index.ts", | |||
{ | |||
configPath: "${child.replaceAll("\\", "/")}/wrangler.toml", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It turns out that:
a) the configPath
property is not valid so was not being used
b) Wrangler was using the script path above to infer the location of the config path
I could have changed the configPath
property to config
which is more correct, but actually not having this here at all is a good check that we are resolving the config by "finding up" from the script path (rather than the current working directory).
@@ -8,6 +8,9 @@ import WebSocket from "ws"; | |||
import { WranglerE2ETestHelper } from "./helpers/e2e-wrangler-test"; | |||
import { generateResourceName } from "./helpers/generate-resource-name"; | |||
|
|||
const port = await getPort(); | |||
const inspectorPort = await getPort(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only changes in this file are to explicitly set both the port and inspector port to avoid concurrent tests from failing due to port 9229 being blocked.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These tests don't run concurrently in CI—was this a problem you were seeing locally?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah it was when I running tests locally.
await helper.seed({ | ||
"_worker.js": dedent` | ||
const port = await getPort(); | ||
const inspectorPort = await getPort(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only changes in this file are to explicitly set both the port and inspector port to avoid concurrent tests from failing due to port 9229 being blocked.
@@ -327,7 +330,7 @@ function applyPythonConfig( | |||
config: Config, | |||
args: NormalizeAndValidateConfigArgs | |||
) { | |||
const mainModule = "script" in args ? args.script : config.main; | |||
const mainModule = args.script ?? config.main; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is possible that the args.script
property exists but is set to undefined
, which means that we should still defer to config.main
in that case.
f33b32a
to
13d920f
Compare
This creates a lot of unwanted logging in the console. The logging is still dumped to the console if the test times out, or if `WRANGLER_LOG=debug` is set as env var.
… running deploy commands This new feature is designed for build tools and frameworks to provide a deploy-specific configuration, which Wrangler can use instead of user configuration when running deploy commands. It is not expected that developers of Workers will need to use this feature directly. The commands that use this feature are: - `wrangler deploy` - `wrangler dev` - `wrangler versions upload` - `wrangler versions deploy` When running these commands, Wrangler will look up the directory tree from the current working directory for a file at the path `.wrangler/deploy/config.json`. This file must contain only a single JSON object of the form: ```json { "configPath": "../../path/to/wrangler.json" } ``` When this file exists Wrangler will use the `configPath` (relative to the `deploy.json` file) to find an alternative Wrangler configuration file to load and use as part of this command. When this happens Wrangler will display a warning to the user to indicate that the configuration has been redirected to a different file than the user's configuration file. A common approach that a build tool might choose to implement. - The user writes code that uses Cloudflare Workers resources, configured via a user `wrangler.toml` file. ```toml name = "my-worker" main = "src/index.ts" [[kv_namespaces]] binding = "<BINDING_NAME1>" id = "<NAMESPACE_ID1>" ``` Note that this configuration points `main` at user code entry-point. - The user runs a custom build, which might read the `wrangler.toml` to find the entry-point: ```bash > my-tool build ``` - This tool generates a `dist` directory that contains both compiled code and a new deployment configuration file, but also a `.wrangler/deploy/config.json` file that redirects Wrangler to this new deployment configuration file: ```plain - dist - index.js - wrangler.json - .wrangler - deploy - config.json ``` The `dist/wrangler.json` will contain: ```json { "name": "my-worker", "main": "./index.js", "kv_namespaces": [{ "binding": "<BINDING_NAME1>", "id": "<NAMESPACE_ID1>" }] } ``` And the `.wrangler/deploy/config.json` will contain: ```json { "configPath": "../../dist/wrangler.json" } ```
…ectory containing: ``` CLOUDFLARE_ACCOUNT_ID=<dev-prod-account-id> CLOUDFLARE_API_TOKEN=<dev-prod-account-api-key> WRANGLER="node <path to workers-sdk/packages/wrangler/bin/wrangler.js>" WRANGLER_IMPORT="<path to /workers-sdk/packages/wrangler/wrangler-dist/cli.js>" ````
… relative pages_build_output_dir path
…` contains absolute paths.
…onfigRedirectIfAvailable"
This fixture does not need a package lockfile to work.
244c408
to
e153667
Compare
packages/wrangler/src/__tests__/config/findWranglerConfig.test.ts
Outdated
Show resolved
Hide resolved
config?: string; | ||
script?: string; | ||
}, | ||
options: { useRedirectIfAvailable?: boolean } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this (and the default value/behavior) be documented?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes documentation is in cloudflare/cloudflare-docs#18757
): string | undefined { | ||
return ( | ||
referencePath: string = process.cwd(), | ||
{ useRedirectIfAvailable = false } = {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
document this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will be in cloudflare/cloudflare-docs#18757
Fixes #5579
This feature is designed for build tools and frameworks to provide a deploy-specific configuration, which Wrangler can use instead of user configuration when running deploy commands.
It is not expected that developers of Workers will need to use this feature directly.
Affected commands
The commands that use this feature are:
wrangler deploy
wrangler dev
wrangler versions upload
wrangler versions deploy
wrangler pages deploy
wrangler pages build
wrangler pages build-env
Config redirect file
When running these commands, Wrangler will look up the directory tree from the current working directory for a file at the path
.wrangler/deploy/config.json
. This file must contain only a single JSON object of the form:When this file exists Wrangler will use the
configPath
(relative to theconfig.json
file) to find an alternative Wrangler configuration file to load and use as part of this command.When this happens Wrangler will display a warning to the user to indicate that the configuration has been redirected to a different file than the user's configuration file.
Custom build tool example
A common approach that a build tool might choose to implement.
The user writes code that uses Cloudflare Workers resources, configured via a user
wrangler.toml
file.Note that this configuration points
main
at user code entry-point.The user runs a custom build, which might read the
wrangler.toml
to find the entry-point:> my-tool build
This tool generates a
dist
directory that contains both compiled code and a new deployment configuration file, but also a.wrangler/deploy/config.json
file that redirects Wrangler to this new deployment configuration file:The
dist/wrangler.json
will contain:And the
.wrangler/deploy/config.json
will contain: