Skip to content

buildEnv: "auto" reads the wrangler config from the working directory, not the Vite root #1037

Closed

Description

@ivogt

rango({ preset: "cloudflare", buildEnv: "auto" }) builds its build-time environment from the wrangler config found from the directory vite build is run in, not from the Vite root. Building an app from another working directory gives build-time handlers an env without the app's bindings.

Where

resolveBuildEnv in packages/rangojs-router/src/vite/router-discovery.ts (line 351 on main, eed288b1):

const userRequire = createRequire(resolve(factoryCtx.root, "package.json"));
const wranglerPath = userRequire.resolve("wrangler");
const { getPlatformProxy } = await import(pathToFileURL(wranglerPath).href);
const proxy = await getPlatformProxy(); // no configPath

Wrangler itself is resolved from the project root. The config is not: getPlatformProxy() without configPath searches from process.cwd().

Observed

vite build tests/cloudflare-basic from the repository root fails in the build-time prerender of /build-env (src/pages/build-env-handler.tsx, await ctx.env.KV.put(...)):

Cannot read properties of undefined (reading 'put')

vite build inside tests/cloudflare-basic passes. Seen while taking the two-working-directory sample for #1034, on that branch and on its base commit (main plus one docs commit). The cause above is read from the code; the failing build was not bisected further.

Expected

The same build from any working directory. Pass configPath resolved against the Vite root (factoryCtx.root); if the Cloudflare Vite plugin is configured with its own configPath, that one is the better source.

Test

A build from a second working directory. #1034 adds one for the node fixture (cache-versions-build.test.ts, vite build app from the parent directory); tests/cloudflare-basic is left out of that PR's two-directory sample because of this bug.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions