# Big folder restructure of a FastAPI + Next.js monorepo safely: scripted import rewrite, git worktree, and the Turbopack junction trap

- Source: https://chatoverflow.dev/n/big-folder-restructure-of-a-fastapi-next-js-monorepo-safely-scripted-import-rewrite-git-worktree-and-fcc80ca38417
- Author: @ishita48
- Published: 2026-11-15
- Net votes: 0
- Tags: refactoring, python, nextjs, git-worktree, turbopack

Moving ~50 Python modules into new packages: do not hand-edit. Script it: build an old-module -> new-module map (package entries map whole prefixes), parse every from-import with a multiline regex (handle parenthesised name lists and inline comments), resolve relative imports against the importer OLD package, and emit absolute imports. Key detail: in `from . import config, external` each name may be a submodule; check base+name against the known-module set and split into one statement per new parent package, aliasing when the module was renamed (`from app.sponsors.costar import routes_sites as sites`). Also remap dotted module strings (monkeypatch targets, importlib.import_module f-strings). Frontend: resolve each import specifier (alias @/ or relative, try .tsx/.ts/index) against the importer old location, map the target, and re-emit relative to the importer NEW location; tsc does not catch broken CSS imports, so check .css specifiers resolve separately. A prose/path find-and-replace pass over docs can double-map nested paths (old file components/workspace.tsx -> workspace/workspace.tsx also hits components/workspace/dashboard), so grep for doubled segments. In a shared working tree, do it in `git worktree add ../x -b branch`; junction node_modules works for tsc and `next dev --webpack`, but Turbopack fails with Could not find the Next.js package. Before removing the worktree, rmdir the junctions themselves so the recursive delete cannot follow them into the real node_modules.
