Skip to content

Commit

Permalink
Fix relative project root (#7285)
Browse files Browse the repository at this point in the history
  • Loading branch information
penalosa authored Nov 20, 2024
1 parent 1b80dec commit fa21312
Show file tree
Hide file tree
Showing 20 changed files with 214 additions and 62 deletions.
5 changes: 5 additions & 0 deletions .changeset/sixty-news-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

Rename `directory` to `projectRoot` and ensure it's relative to the `wrangler.toml`. This fixes a regression which meant that `.wrangler` temporary folders were inadvertently generated relative to `process.cwd()` rather than the location of the `wrangler.toml` file. It also renames `directory` to `projectRoot`, which affects the `unstable_startWorker() interface.
6 changes: 3 additions & 3 deletions packages/create-cloudflare/e2e-tests/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ describe.skipIf(experimental || frameworkToTest || isQuarantineMode())(
const { output } = await runC3(
[
project.path,
"--template=https://github.com/cloudflare/templates/worker-router",
"--template=https://github.com/cloudflare/workers-graphql-server",
"--no-deploy",
"--git=false",
],
Expand All @@ -208,10 +208,10 @@ describe.skipIf(experimental || frameworkToTest || isQuarantineMode())(
);

expect(output).toContain(
`repository https://github.com/cloudflare/templates/worker-router`,
`repository https://github.com/cloudflare/workers-graphql-server`,
);
expect(output).toContain(
`Cloning template from: https://github.com/cloudflare/templates/worker-router`,
`Cloning template from: https://github.com/cloudflare/workers-graphql-server`,
);
expect(output).toContain(`template cloned and validated`);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function configDefaults(
const persist = path.join(process.cwd(), ".wrangler/persist");
return {
entrypoint: "NOT_REAL",
directory: "NOT_REAL",
projectRoot: "NOT_REAL",
build: unusable<StartDevWorkerOptions["build"]>(),
legacy: {},
dev: { persist },
Expand Down Expand Up @@ -68,7 +68,7 @@ describe("BundleController", () => {
legacy: {},
name: "worker",
entrypoint: path.resolve("src/index.ts"),
directory: path.resolve("src"),
projectRoot: path.resolve("src"),
build: {
additionalModules: [],
processEntrypoint: false,
Expand Down Expand Up @@ -141,7 +141,7 @@ describe("BundleController", () => {
legacy: {},
name: "worker",
entrypoint: path.resolve("src/index.ts"),
directory: path.resolve("src"),
projectRoot: path.resolve("src"),
build: {
additionalModules: [],
processEntrypoint: false,
Expand Down Expand Up @@ -207,7 +207,7 @@ describe("BundleController", () => {
legacy: {},
name: "worker",
entrypoint: path.resolve("out.ts"),
directory: path.resolve("."),
projectRoot: path.resolve("."),
build: {
additionalModules: [],
processEntrypoint: false,
Expand Down Expand Up @@ -287,7 +287,7 @@ describe("BundleController", () => {
legacy: {},
name: "worker",
entrypoint: path.resolve("src/index.ts"),
directory: path.resolve("src"),
projectRoot: path.resolve("src"),
build: {
additionalModules: [],
processEntrypoint: false,
Expand Down Expand Up @@ -345,7 +345,7 @@ describe("BundleController", () => {
legacy: {},
name: "worker",
entrypoint: path.resolve("src/index.ts"),
directory: path.resolve("src"),
projectRoot: path.resolve("src"),

build: {
additionalModules: [],
Expand Down Expand Up @@ -391,7 +391,7 @@ describe("BundleController", () => {
const configCustom: Partial<StartDevWorkerOptions> = {
name: "worker",
entrypoint: path.resolve("out.ts"),
directory: process.cwd(),
projectRoot: process.cwd(),
build: {
additionalModules: [],
processEntrypoint: false,
Expand Down Expand Up @@ -464,7 +464,7 @@ describe("BundleController", () => {
const configCustom: Partial<StartDevWorkerOptions> = {
name: "worker",
entrypoint: path.resolve("out.ts"),
directory: process.cwd(),
projectRoot: process.cwd(),

build: {
additionalModules: [],
Expand Down Expand Up @@ -513,7 +513,7 @@ describe("BundleController", () => {
legacy: {},
name: "worker",
entrypoint: path.resolve("src/index.ts"),
directory: path.resolve("src"),
projectRoot: path.resolve("src"),

build: {
additionalModules: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe("ConfigController", () => {
moduleRoot: path.join(process.cwd(), "src"),
moduleRules: [],
},
directory: process.cwd(),
projectRoot: process.cwd(),
entrypoint: path.join(process.cwd(), "src/index.ts"),
},
});
Expand Down Expand Up @@ -87,7 +87,7 @@ base_dir = \"./some/base_dir\"`,
moduleRoot: path.join(process.cwd(), "./some/base_dir"),
moduleRules: [],
},
directory: process.cwd(),
projectRoot: process.cwd(),
entrypoint: path.join(process.cwd(), "./some/base_dir/nested/index.js"),
},
});
Expand All @@ -114,7 +114,7 @@ base_dir = \"./some/base_dir\"`,
type: "configUpdate",
config: {
entrypoint: path.join(process.cwd(), "src/index.ts"),
directory: process.cwd(),
projectRoot: process.cwd(),
build: {
additionalModules: [],
define: {},
Expand All @@ -138,7 +138,7 @@ base_dir = \"./some/base_dir\"`,
type: "configUpdate",
config: {
entrypoint: path.join(process.cwd(), "src/index.ts"),
directory: process.cwd(),
projectRoot: process.cwd(),
build: {
additionalModules: [],
define: {},
Expand Down Expand Up @@ -168,7 +168,7 @@ base_dir = \"./some/base_dir\"`,
type: "configUpdate",
config: {
entrypoint: path.join(process.cwd(), "src/index.ts"),
directory: process.cwd(),
projectRoot: process.cwd(),
build: {
alias: {
foo: "bar",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function makeEsbuildBundle(testBundle: TestBundle): Bundle {
entrypointSource: "",
entry: {
file: "index.mjs",
directory: "/virtual/",
projectRoot: "/virtual/",
format: "modules",
moduleRoot: "/virtual",
name: undefined,
Expand Down Expand Up @@ -127,7 +127,7 @@ function configDefaults(
): StartDevWorkerOptions {
return {
entrypoint: "NOT_REAL",
directory: "NOT_REAL",
projectRoot: "NOT_REAL",
build: unusable<StartDevWorkerOptions["build"]>(),
legacy: {},
dev: { persist: "./persist" },
Expand Down Expand Up @@ -232,7 +232,7 @@ describe("LocalRuntimeController", () => {
`,
entry: {
file: "esm/index.mjs",
directory: "/virtual/",
projectRoot: "/virtual/",
format: "modules",
moduleRoot: "/virtual",
name: undefined,
Expand Down Expand Up @@ -346,7 +346,7 @@ describe("LocalRuntimeController", () => {
path: "/virtual/index.js",
entry: {
file: "index.js",
directory: "/virtual/",
projectRoot: "/virtual/",
format: "service-worker",
moduleRoot: "/virtual",
name: undefined,
Expand Down
14 changes: 7 additions & 7 deletions packages/wrangler/src/__tests__/find-additional-modules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe("traverse module graph", () => {
const modules = await findAdditionalModules(
{
file: path.join(process.cwd(), "./index.js"),
directory: process.cwd(),
projectRoot: process.cwd(),
format: "modules",
moduleRoot: process.cwd(),
exports: [],
Expand Down Expand Up @@ -75,7 +75,7 @@ describe("traverse module graph", () => {
const modules = await findAdditionalModules(
{
file: path.join(process.cwd(), "./index.js"),
directory: process.cwd(),
projectRoot: process.cwd(),
format: "modules",
moduleRoot: process.cwd(),
exports: [],
Expand Down Expand Up @@ -109,7 +109,7 @@ describe("traverse module graph", () => {
const modules = await findAdditionalModules(
{
file: path.join(process.cwd(), "./src/nested/index.js"),
directory: path.join(process.cwd(), "./src/nested"),
projectRoot: path.join(process.cwd(), "./src/nested"),
format: "modules",
// The default module root is dirname(file)
moduleRoot: path.join(process.cwd(), "./src/nested"),
Expand Down Expand Up @@ -144,7 +144,7 @@ describe("traverse module graph", () => {
const modules = await findAdditionalModules(
{
file: path.join(process.cwd(), "./src/nested/index.js"),
directory: path.join(process.cwd(), "./src/nested"),
projectRoot: path.join(process.cwd(), "./src/nested"),
format: "modules",
// The default module root is dirname(file)
moduleRoot: path.join(process.cwd(), "./src"),
Expand Down Expand Up @@ -179,7 +179,7 @@ describe("traverse module graph", () => {
const modules = await findAdditionalModules(
{
file: path.join(process.cwd(), "./src/nested/index.js"),
directory: path.join(process.cwd(), "./src/nested"),
projectRoot: path.join(process.cwd(), "./src/nested"),
format: "modules",
// The default module root is dirname(file)
moduleRoot: path.join(process.cwd(), "./src"),
Expand Down Expand Up @@ -214,7 +214,7 @@ describe("traverse module graph", () => {
const modules = await findAdditionalModules(
{
file: path.join(process.cwd(), "./src/index.js"),
directory: path.join(process.cwd(), "./src"),
projectRoot: path.join(process.cwd(), "./src"),
format: "modules",
// The default module root is dirname(file)
moduleRoot: path.join(process.cwd(), "./src"),
Expand Down Expand Up @@ -249,7 +249,7 @@ describe("traverse module graph", () => {
findAdditionalModules(
{
file: path.join(process.cwd(), "./src/index.js"),
directory: path.join(process.cwd(), "./src"),
projectRoot: path.join(process.cwd(), "./src"),
format: "modules",
// The default module root is dirname(file)
moduleRoot: path.join(process.cwd(), "./src"),
Expand Down
143 changes: 143 additions & 0 deletions packages/wrangler/src/__tests__/get-entry.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import path from "path";
import dedent from "ts-dedent";
import { defaultWranglerConfig } from "../config/config";
import { getEntry } from "../deployment-bundle/entry";
import { mockConsoleMethods } from "./helpers/mock-console";
import { runInTempDir } from "./helpers/run-in-tmp";
import { seed } from "./helpers/seed";
import type { Entry } from "../deployment-bundle/entry";

function normalize(entry: Entry): Entry {
const tmpDir = process.cwd();
const tmpDirName = path.basename(tmpDir);

return Object.fromEntries(
Object.entries(entry).map(([k, v]) => [
k,
typeof v === "string"
? v
.replaceAll("\\", "/")
.replace(new RegExp(`(.*${tmpDirName})`), `/tmp/dir`)
: v,
])
) as Entry;
}

describe("getEntry()", () => {
runInTempDir();
mockConsoleMethods();

it("--script index.ts", async () => {
await seed({
"index.ts": dedent/* javascript */ `
export default {
fetch() {
}
}
`,
});
const entry = await getEntry(
{ script: "index.ts" },
defaultWranglerConfig,
"deploy"
);
expect(normalize(entry)).toMatchObject({
projectRoot: "/tmp/dir",
file: "/tmp/dir/index.ts",
moduleRoot: "/tmp/dir",
});
});

it("--script src/index.ts", async () => {
await seed({
"src/index.ts": dedent/* javascript */ `
export default {
fetch() {
}
}
`,
});
const entry = await getEntry(
{ script: "src/index.ts" },
defaultWranglerConfig,
"deploy"
);
expect(normalize(entry)).toMatchObject({
projectRoot: "/tmp/dir",
file: "/tmp/dir/src/index.ts",
moduleRoot: "/tmp/dir/src",
});
});

it("main = index.ts", async () => {
await seed({
"index.ts": dedent/* javascript */ `
export default {
fetch() {
}
}
`,
});
const entry = await getEntry(
{},
{ ...defaultWranglerConfig, main: "index.ts" },
"deploy"
);
expect(normalize(entry)).toMatchObject({
projectRoot: "/tmp/dir",
file: "/tmp/dir/index.ts",
moduleRoot: "/tmp/dir",
});
});

it("main = src/index.ts", async () => {
await seed({
"src/index.ts": dedent/* javascript */ `
export default {
fetch() {
}
}
`,
});
const entry = await getEntry(
{},
{ ...defaultWranglerConfig, main: "src/index.ts" },
"deploy"
);
expect(normalize(entry)).toMatchObject({
projectRoot: "/tmp/dir",
file: "/tmp/dir/src/index.ts",
moduleRoot: "/tmp/dir/src",
});
});

it("main = src/index.ts w/ configPath", async () => {
await seed({
"other-worker/src/index.ts": dedent/* javascript */ `
export default {
fetch() {
}
}
`,
});
const entry = await getEntry(
{},
{
...defaultWranglerConfig,
main: "src/index.ts",
configPath: "other-worker/wrangler.toml",
},
"deploy"
);
expect(normalize(entry)).toMatchObject({
projectRoot: "/tmp/dir/other-worker",
file: "/tmp/dir/other-worker/src/index.ts",
moduleRoot: "/tmp/dir/other-worker/src",
});
});
});
Loading

0 comments on commit fa21312

Please sign in to comment.