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: default watch_dir to src of project directory #646

Merged
merged 1 commit into from
Mar 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/perfect-cars-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"wrangler": patch
---

fix: default `watch_dir` to `src` of project directory

Via wrangler 1, when using custom builds in `wrangler dev`, `watch_dir` should default to `src` of the "project directory" (i.e - wherever the `wrangler.toml` is defined if it exists, else in the cwd.

Fixes https://github.com/cloudflare/wrangler2/issues/631
47 changes: 47 additions & 0 deletions packages/wrangler/src/__tests__/configuration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,53 @@ describe("normalizeAndValidateConfig()", () => {
`);
});

it("should default custom build watch directories to src", () => {
const expectedConfig: RawConfig = {
build: {
command: "execute some --build",
},
};

const { config, diagnostics } = normalizeAndValidateConfig(
expectedConfig,
undefined
);

expect(config.build).toEqual(
expect.objectContaining({
command: "execute some --build",
watch_dir: "./src",
})
);

expect(diagnostics.hasErrors()).toBe(false);
expect(diagnostics.hasWarnings()).toBe(false);
});

it("should resolve custom build watch directories relative to wrangler.toml", async () => {
const expectedConfig: RawConfig = {
build: {
command: "execute some --build",
watch_dir: "some/path",
},
};

const { config, diagnostics } = normalizeAndValidateConfig(
expectedConfig,
"project/wrangler.toml"
);

expect(config.build).toEqual(
expect.objectContaining({
command: "execute some --build",
watch_dir: path.normalize("project/some/path"),
})
);

expect(diagnostics.hasErrors()).toBe(false);
expect(diagnostics.hasWarnings()).toBe(false);
});

it("should override `migrations` config defaults with provided values", () => {
const expectedConfig: RawConfig = {
migrations: [
Expand Down
2 changes: 1 addition & 1 deletion packages/wrangler/src/__tests__/dev.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ describe("Dev component", () => {
command:
"node -e \"console.log('custom build'); require('fs').writeFileSync('index.js', 'export default { fetch(){ return new Response(123) } }')\"",
cwd: undefined,
watch_dir: undefined,
watch_dir: "src",
});
expect(std.out).toMatchInlineSnapshot(
`"running: node -e \\"console.log('custom build'); require('fs').writeFileSync('index.js', 'export default { fetch(){ return new Response(123) } }')\\""`
Expand Down
26 changes: 23 additions & 3 deletions packages/wrangler/src/config/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ export function normalizeAndValidateConfig(
const { deprecatedUpload, ...build } = normalizeAndValidateBuild(
diagnostics,
rawConfig,
rawConfig.build ?? {}
rawConfig.build ?? {},
configPath
);

validateOptionalProperty(
Expand Down Expand Up @@ -165,7 +166,8 @@ export function normalizeAndValidateConfig(
function normalizeAndValidateBuild(
diagnostics: Diagnostics,
rawConfig: RawConfig,
rawBuild: Config["build"]
rawBuild: Config["build"],
configPath: string | undefined
): Config["build"] & { deprecatedUpload: DeprecatedUpload } {
const { command, cwd, watch_dir, upload, ...rest } = rawBuild;
const deprecatedUpload: DeprecatedUpload = { ...upload };
Expand Down Expand Up @@ -220,7 +222,25 @@ function normalizeAndValidateBuild(
);
}

return { command, cwd, watch_dir, deprecatedUpload };
return {
command,
watch_dir:
// - `watch_dir` only matters when `command` is defined, so we apply
// a default only when `command` is defined
// - `configPath` will always be defined since `build` can only
// be configured in `wrangler.toml`, but who knows, that may
// change in the future, so we do a check anyway
command
? configPath
? path.relative(
process.cwd(),
Copy link
Contributor

Choose a reason for hiding this comment

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

Generally I think we need to be making these paths relative to the wrangler.toml. The reason is that otherwise the following would have different results:

wrangler -c path/to/wrangler.toml ...
cd path/to
wrangler -c wrangler.toml

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sorry, I don't understand. what would be the difference with these two? they would both resolve a watch_dir config to the same path, unless I'm missing something

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it does resolve against wrangler.toml's path in the next line

Copy link
Contributor

Choose a reason for hiding this comment

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

OK, so I see my confusion now. You are saying that the configPath is always, already, relative to the cwd() (or it is absolute).

In that case there is no need for the relative(process.cwd(), ...) bit then, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just a personal preference, I don't like seeing absolute paths if and when I log this during development. Is that a problem?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is exactly how we normalised paths for wasm_modules and text_blobs too

Copy link
Contributor

Choose a reason for hiding this comment

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

OK - it just really confused me, because if it is possible that configPath is not relative to src then this would break.
Happy to go with this for the timebeing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah of the wrangler.Tom isn't adjacent to a src directory then this won't work. Honestly I think there shouldn't have been a default for that field at all (much like upload.dir)

path.join(path.dirname(configPath), watch_dir || "./src")
)
: watch_dir || "./src"
: watch_dir,
cwd,
deprecatedUpload,
};
}

/**
Expand Down