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 relative project root #7285

Merged
merged 9 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
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.
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
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()", () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

🙏 🙇 ♥️

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",
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ describe("defineNavigatorUserAgent is respected", () => {
await bundleWorker(
{
file: path.resolve("src/index.js"),
directory: process.cwd(),
projectRoot: process.cwd(),
format: "modules",
moduleRoot: path.dirname(path.resolve("src/index.js")),
exports: [],
Expand Down Expand Up @@ -167,7 +167,7 @@ describe("defineNavigatorUserAgent is respected", () => {
await bundleWorker(
{
file: path.resolve("src/index.js"),
directory: process.cwd(),
projectRoot: process.cwd(),
format: "modules",
moduleRoot: path.dirname(path.resolve("src/index.js")),
exports: [],
Expand Down
Loading
Loading