From fb6547e644f337f07f69628b81f1f90e7a9a7a72 Mon Sep 17 00:00:00 2001 From: Sunil Pai Date: Thu, 7 Apr 2022 09:19:59 -0500 Subject: [PATCH] fix: use cwd for `--experiment-enable-local-persistence` This sets up `--experiment-enable-local-persistence` to explicitly use `process.cwd() + wrangler-local-state` as a path to store values. Without it, local mode uses the temp dir that we use to bundle the worker, which gets wiped out on ending wrangler dev. In the future, based on usage, we may want to make the path configurable as well. Fixes https://github.com/cloudflare/wrangler2/issues/766 --- .changeset/chatty-plants-pump.md | 9 +++++++++ packages/wrangler/src/dev/local.tsx | 20 ++++++++++++++++---- 2 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 .changeset/chatty-plants-pump.md diff --git a/.changeset/chatty-plants-pump.md b/.changeset/chatty-plants-pump.md new file mode 100644 index 0000000000000..a8c2df2af2a63 --- /dev/null +++ b/.changeset/chatty-plants-pump.md @@ -0,0 +1,9 @@ +--- +"wrangler": patch +--- + +fix: use cwd for `--experiment-enable-local-persistence` + +This sets up `--experiment-enable-local-persistence` to explicitly use `process.cwd() + wrangler-local-state` as a path to store values. Without it, local mode uses the temp dir that we use to bundle the worker, which gets wiped out on ending wrangler dev. In the future, based on usage, we may want to make the path configurable as well. + +Fixes https://github.com/cloudflare/wrangler2/issues/766 diff --git a/packages/wrangler/src/dev/local.tsx b/packages/wrangler/src/dev/local.tsx index a9e61a8d12503..6eb3c75e6c385 100644 --- a/packages/wrangler/src/dev/local.tsx +++ b/packages/wrangler/src/dev/local.tsx @@ -59,6 +59,18 @@ function useLocalWorker({ const local = useRef>(); const removeSignalExitListener = useRef<() => void>(); const [inspectorUrl, setInspectorUrl] = useState(); + // if we're using local persistence for data, we should use the cwd + // as an explicit path, or else it'll use the temp dir + // which disappears when dev ends + const enableLocalPersistencePathOrDisable = enableLocalPersistence + ? // Maybe we could make the path configurable as well? + path.join(process.cwd(), "wrangler-local-state") + : // We have to explicitly choose not to use local persistence, + // since it defaults to true. That said, a benefit of just enabling + // it as is, would be that it would persist for all of + // `wrangler dev` surviving, which may be useful anyway. + // TODO: We should revisit this based on usage. + false; useEffect(() => { const abortController = new AbortController(); async function startLocalWorker() { @@ -160,14 +172,14 @@ function useLocalWorker({ compatibilityDate, compatibilityFlags, kvNamespaces: bindings.kv_namespaces?.map((kv) => kv.binding), - kvPersist: enableLocalPersistence, + kvPersist: enableLocalPersistencePathOrDisable, durableObjects: Object.fromEntries( (bindings.durable_objects?.bindings ?? []).map<[string, string]>( (value) => [value.name, value.class_name] ) ), - durableObjectsPersist: enableLocalPersistence, - cachePersist: enableLocalPersistence, + durableObjectsPersist: enableLocalPersistencePathOrDisable, + cachePersist: enableLocalPersistencePathOrDisable, sitePath: assetPaths?.assetDirectory ? path.join(assetPaths.baseDirectory, assetPaths.assetDirectory) : undefined, @@ -274,7 +286,7 @@ function useLocalWorker({ bindings.vars, compatibilityDate, compatibilityFlags, - enableLocalPersistence, + enableLocalPersistencePathOrDisable, assetPaths, publicDirectory, rules,