From 30fbfabcb706330ead6368f0db11a5e373b4463c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isaac=20Rold=C3=A1n?= Date: Fri, 23 Oct 2026 11:14:36 +0200 Subject: [PATCH 1/3] Exclude build output and temporary files from app dev watchers Limit Cargo output ignores to Rust function roots so source files and imports under similarly named paths remain watched. Co-authored-by: River --- .../app-dev-watcher-ignore-build-output.md | 5 + .../dev/app-events/file-watcher.test.ts | 124 +++++++++++++++++- .../services/dev/app-events/file-watcher.ts | 32 ++++- 3 files changed, 157 insertions(+), 4 deletions(-) create mode 100644 .changeset/app-dev-watcher-ignore-build-output.md diff --git a/.changeset/app-dev-watcher-ignore-build-output.md b/.changeset/app-dev-watcher-ignore-build-output.md new file mode 100644 index 00000000000..221ea490026 --- /dev/null +++ b/.changeset/app-dev-watcher-ignore-build-output.md @@ -0,0 +1,5 @@ +--- +'@shopify/app': patch +--- + +Reduce `app dev` file watching by excluding Rust function build output, `.shopify` files, and editor temporary files. diff --git a/packages/app/src/cli/services/dev/app-events/file-watcher.test.ts b/packages/app/src/cli/services/dev/app-events/file-watcher.test.ts index 5fad96b49d0..6d6374d5a7d 100644 --- a/packages/app/src/cli/services/dev/app-events/file-watcher.test.ts +++ b/packages/app/src/cli/services/dev/app-events/file-watcher.test.ts @@ -9,10 +9,10 @@ import { } from '../../../models/app/app.test-data.js' import {flushPromises} from '@shopify/cli-kit/node/promises' import {describe, expect, test, vi} from 'vitest' -import chokidar from 'chokidar' +import chokidar, {FSWatcher} from 'chokidar' import {AbortController} from '@shopify/cli-kit/node/abort' import {inTemporaryDirectory, mkdir, writeFile, fileExistsSync} from '@shopify/cli-kit/node/fs' -import {joinPath, normalizePath} from '@shopify/cli-kit/node/path' +import {dirname, joinPath, normalizePath} from '@shopify/cli-kit/node/path' import {extractImportPathsRecursively} from '@shopify/cli-kit/node/import-extractor' // Mock the import extractor - will be configured per test @@ -22,6 +22,8 @@ vi.mock('@shopify/cli-kit/node/import-extractor', () => ({ extractJSImports: vi.fn(() => []), })) +const realChokidarWatch = chokidar.watch.bind(chokidar) + // Helper to mock watchedFiles for extensions function mockExtensionWatchedFiles(extension: any, files: string[] = []) { vi.spyOn(extension, 'watchedFiles').mockReturnValue(files) @@ -221,7 +223,15 @@ describe('file-watcher events', () => { // Then expect(watchSpy).toHaveBeenCalledWith([joinPath(dir, 'shopify.app.toml'), joinPath(dir, 'extensions')], { - ignored: ['**/node_modules/**', '**/.git/**'], + ignored: [ + '**/node_modules/**', + '**/.git/**', + '**/.shopify/**', + '**/.DS_Store', + '**/*.swp', + '**/*.swo', + '**/*~', + ], ignoreInitial: true, persistent: true, }) @@ -712,6 +722,114 @@ describe('file-watcher events', () => { }) }) + test('chokidar excludes build output and temporary files while preserving source and imports', async () => { + await inTemporaryDirectory(async (dir) => { + // Given + const files = [ + // Rust function: Cargo's target directory is build output + 'extensions/rust-function/Cargo.toml', + 'extensions/rust-function/src/main.rs', + 'extensions/rust-function/target/debug/build/output.rlib', + 'extensions/rust-function/target/release/build/output.rlib', + 'extensions/rust-function/target/wasm32-wasip1/release/rust-function.wasm', + 'extensions/rust-function/target/wasm32-wasi/release/rust-function.wasm', + 'extensions/rust-function/target/rust-analyzer/flycheck0/stdout', + // UI extension: a source folder named target, and dist, stay watched + 'extensions/ui-extension/src/target/index.js', + 'extensions/ui-extension/src/target/debug/index.js', + 'extensions/ui-extension/target/release/index.js', + 'extensions/rust-function/src/target/debug/source.rs', + 'shared/target/release/index.js', + 'extensions/ui-extension/dist/main.js', + 'extensions/ui-extension/generated/main.js', + 'extensions/ui-extension/.DS_Store', + 'extensions/ui-extension/src/index.js.swp', + 'extensions/ui-extension/src/index.js.swo', + 'extensions/ui-extension/src/index.js~', + 'extensions/.shopify/dev-bundle/output.js', + ] + await Promise.all( + files.map(async (file) => { + await mkdir(dirname(joinPath(dir, file))) + await writeFile(joinPath(dir, file), '') + }), + ) + const configPath = joinPath(dir, 'shopify.app.toml') + await writeFile(configPath, '') + + const rustFunction = await testFunctionExtension({dir: joinPath(dir, 'extensions/rust-function')}) + const uiExtension = await testUIExtension({directory: joinPath(dir, 'extensions/ui-extension')}) + mockExtensionWatchedFiles(uiExtension, [joinPath(dir, 'shared/target/release/index.js')]) + const app = testAppLinked({ + allExtensions: [rustFunction, uiExtension], + directory: dir, + configPath, + }) + + let watcher: FSWatcher | undefined + let ready = false + let watcherError: Error | undefined + vi.spyOn(chokidar, 'watch').mockImplementation((paths, options) => { + const realWatcher = realChokidarWatch(paths, options) + realWatcher.once('ready', () => { + ready = true + }) + realWatcher.on('error', (error) => { + watcherError = error + }) + watcher = realWatcher + return realWatcher + }) + + // When + const abortController = new AbortController() + const fileWatcher = new FileWatcher(app, {...outputOptions, signal: abortController.signal}) + try { + await fileWatcher.start() + await vi.waitFor(() => { + if (watcherError) throw watcherError + expect(ready).toBe(true) + }) + + // Then + const watched = Object.entries(watcher!.getWatched()).flatMap(([directory, names]) => + names.map((name) => normalizePath(joinPath(directory, name)).replace(`${normalizePath(dir)}/`, '')), + ) + + expect(watched).toEqual( + expect.arrayContaining([ + 'extensions/rust-function/Cargo.toml', + 'extensions/rust-function/src/main.rs', + 'extensions/ui-extension/src/target/index.js', + 'extensions/ui-extension/src/target/debug/index.js', + 'extensions/ui-extension/target/release/index.js', + 'extensions/rust-function/src/target/debug/source.rs', + 'shared/target/release/index.js', + 'extensions/ui-extension/dist/main.js', + 'extensions/ui-extension/generated/main.js', + ]), + ) + for (const ignored of [ + 'extensions/rust-function/target/debug', + 'extensions/rust-function/target/release', + 'extensions/rust-function/target/wasm32-wasip1', + 'extensions/rust-function/target/wasm32-wasi', + 'extensions/rust-function/target/rust-analyzer', + 'extensions/ui-extension/.DS_Store', + 'extensions/ui-extension/src/index.js.swp', + 'extensions/ui-extension/src/index.js.swo', + 'extensions/ui-extension/src/index.js~', + 'extensions/.shopify', + ]) { + expect(watched.filter((path) => path === ignored || path.startsWith(`${ignored}/`))).toEqual([]) + } + } finally { + abortController.abort() + await watcher?.close() + } + }) + }) + describe('runtime file discovery', () => { test('files added at runtime inside an existing extension trigger file_created', async () => { await inTemporaryDirectory(async (dir) => { diff --git a/packages/app/src/cli/services/dev/app-events/file-watcher.ts b/packages/app/src/cli/services/dev/app-events/file-watcher.ts index cbc86472baa..4a9f48ab75a 100644 --- a/packages/app/src/cli/services/dev/app-events/file-watcher.ts +++ b/packages/app/src/cli/services/dev/app-events/file-watcher.ts @@ -17,6 +17,26 @@ const EXTENSION_CREATION_TIMEOUT_IN_MS = 60000 const EXTENSION_CREATION_CHECK_INTERVAL_IN_MS = 200 const FILE_DELETE_TIMEOUT_IN_MS = 500 +/** + * Exclude these paths before chokidar traverses them to avoid allocating watchers + * or polling files whose events would be discarded later. + */ +const ALWAYS_IGNORED_PATHS = [ + // Dependencies and version control + '**/node_modules/**', + '**/.git/**', + // Files the CLI writes itself: dev and deploy bundles, certificates, app security results + '**/.shopify/**', + // macOS Finder metadata + '**/.DS_Store', + // Editor swap and backup files + '**/*.swp', + '**/*.swo', + '**/*~', +] + +const CARGO_BUILD_DIRECTORIES = ['debug', 'release', 'wasm32-wasip1', 'wasm32-wasi', 'rust-analyzer'] + /** * Event emitted by the file watcher * @@ -114,12 +134,22 @@ export class FileWatcher { const allWatchedFiles = this.getAllWatchedFiles() watchPaths.push(...allWatchedFiles) + // Anchor Cargo output patterns to Rust function roots so source and imported + // paths such as src/target/debug remain watched. + const cargoIgnoredPaths = this.app.realExtensions + .filter( + (extension) => extension.isFunctionExtension && fileExistsSync(joinPath(extension.directory, 'Cargo.toml')), + ) + .flatMap((extension) => + CARGO_BUILD_DIRECTORIES.map((directory) => joinPath(extension.directory, 'target', directory, '**')), + ) + this.close() // Create new watcher const {default: chokidar} = await import('chokidar') this.watcher = chokidar.watch(watchPaths, { - ignored: ['**/node_modules/**', '**/.git/**'], + ignored: [...ALWAYS_IGNORED_PATHS, ...cargoIgnoredPaths], persistent: true, ignoreInitial: true, }) From 965e470e60ffac39826c81c1271d2028dcff66ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isaac=20Rold=C3=A1n?= Date: Fri, 23 Oct 2026 11:26:03 +0200 Subject: [PATCH 2/3] Ignore complete Cargo output and refresh Function watcher rules --- .../app-dev-watcher-ignore-build-output.md | 2 +- .../dev/app-events/file-watcher.test.ts | 68 +++++++++++++++++-- .../services/dev/app-events/file-watcher.ts | 47 +++++++++---- 3 files changed, 96 insertions(+), 21 deletions(-) diff --git a/.changeset/app-dev-watcher-ignore-build-output.md b/.changeset/app-dev-watcher-ignore-build-output.md index 221ea490026..214f80319db 100644 --- a/.changeset/app-dev-watcher-ignore-build-output.md +++ b/.changeset/app-dev-watcher-ignore-build-output.md @@ -2,4 +2,4 @@ '@shopify/app': patch --- -Reduce `app dev` file watching by excluding Rust function build output, `.shopify` files, and editor temporary files. +Reduce `app dev` file watching by excluding Cargo `target` directories, Function build output, `.shopify` files, and editor temporary files. diff --git a/packages/app/src/cli/services/dev/app-events/file-watcher.test.ts b/packages/app/src/cli/services/dev/app-events/file-watcher.test.ts index 6d6374d5a7d..656f890f103 100644 --- a/packages/app/src/cli/services/dev/app-events/file-watcher.test.ts +++ b/packages/app/src/cli/services/dev/app-events/file-watcher.test.ts @@ -231,6 +231,7 @@ describe('file-watcher events', () => { '**/*.swp', '**/*.swo', '**/*~', + expect.any(Function), ], ignoreInitial: true, persistent: true, @@ -734,6 +735,14 @@ describe('file-watcher events', () => { 'extensions/rust-function/target/wasm32-wasip1/release/rust-function.wasm', 'extensions/rust-function/target/wasm32-wasi/release/rust-function.wasm', 'extensions/rust-function/target/rust-analyzer/flycheck0/stdout', + 'extensions/rust-function/target/.rustc_info.json', + 'extensions/rust-function/target/custom-profile/output.wasm', + 'extensions/rust-function/target/aarch64-unknown-linux-gnu/debug/output.rlib', + 'extensions/js-function/src/index.js', + 'extensions/js-function/dist/function.js', + 'extensions/js-function/dist/index.wasm', + 'workspace/Cargo.toml', + 'workspace/target/custom-profile/output.wasm', // UI extension: a source folder named target, and dist, stay watched 'extensions/ui-extension/src/target/index.js', 'extensions/ui-extension/src/target/debug/index.js', @@ -758,10 +767,15 @@ describe('file-watcher events', () => { await writeFile(configPath, '') const rustFunction = await testFunctionExtension({dir: joinPath(dir, 'extensions/rust-function')}) + const jsFunction = await testFunctionExtension({dir: joinPath(dir, 'extensions/js-function')}) const uiExtension = await testUIExtension({directory: joinPath(dir, 'extensions/ui-extension')}) - mockExtensionWatchedFiles(uiExtension, [joinPath(dir, 'shared/target/release/index.js')]) + mockExtensionWatchedFiles(uiExtension, [ + joinPath(dir, 'shared/target/release/index.js'), + joinPath(dir, 'workspace/Cargo.toml'), + joinPath(dir, 'workspace/target/custom-profile/output.wasm'), + ]) const app = testAppLinked({ - allExtensions: [rustFunction, uiExtension], + allExtensions: [rustFunction, jsFunction, uiExtension], directory: dir, configPath, }) @@ -800,6 +814,8 @@ describe('file-watcher events', () => { expect.arrayContaining([ 'extensions/rust-function/Cargo.toml', 'extensions/rust-function/src/main.rs', + 'extensions/js-function/src/index.js', + 'workspace/Cargo.toml', 'extensions/ui-extension/src/target/index.js', 'extensions/ui-extension/src/target/debug/index.js', 'extensions/ui-extension/target/release/index.js', @@ -810,11 +826,9 @@ describe('file-watcher events', () => { ]), ) for (const ignored of [ - 'extensions/rust-function/target/debug', - 'extensions/rust-function/target/release', - 'extensions/rust-function/target/wasm32-wasip1', - 'extensions/rust-function/target/wasm32-wasi', - 'extensions/rust-function/target/rust-analyzer', + 'extensions/rust-function/target', + 'extensions/js-function/dist', + 'workspace/target', 'extensions/ui-extension/.DS_Store', 'extensions/ui-extension/src/index.js.swp', 'extensions/ui-extension/src/index.js.swo', @@ -830,6 +844,46 @@ describe('file-watcher events', () => { }) }) + test('app reloads refresh Function output ignores without restarting the watcher', async () => { + await inTemporaryDirectory(async (directory) => { + const existingFunction = await testFunctionExtension({dir: joinPath(directory, 'extensions/existing-function')}) + const newFunction = await testFunctionExtension({dir: joinPath(directory, 'extensions/new-function')}) + const app = testAppLinked({ + allExtensions: [existingFunction], + directory, + configPath: joinPath(directory, 'shopify.app.toml'), + }) + const mockWatcher = { + on: vi.fn().mockReturnThis(), + close: vi.fn().mockResolvedValue(undefined), + } + const watchSpy = vi.spyOn(chokidar, 'watch').mockReturnValue(mockWatcher as any) + const abortController = new AbortController() + const fileWatcher = new FileWatcher(app, {...outputOptions, signal: abortController.signal}) + + try { + await fileWatcher.start() + const ignoredPaths = watchSpy.mock.calls[0]![1]!.ignored as (string | ((path: string) => boolean))[] + const ignoreBuildOutput = ignoredPaths.find((pattern) => typeof pattern === 'function') as ( + path: string, + ) => boolean + const existingOutput = joinPath(existingFunction.directory, 'dist/function.js') + const newOutput = joinPath(newFunction.directory, 'dist/function.js') + expect(ignoreBuildOutput(existingOutput)).toBe(true) + expect(ignoreBuildOutput(newOutput)).toBe(false) + + fileWatcher.updateApp(testAppLinked({...app, allExtensions: [newFunction]})) + + expect(ignoreBuildOutput(newOutput)).toBe(true) + expect(ignoreBuildOutput(existingOutput)).toBe(false) + expect(ignoreBuildOutput(joinPath(newFunction.directory, 'src/index.js'))).toBe(false) + expect(watchSpy).toHaveBeenCalledTimes(1) + } finally { + abortController.abort() + } + }) + }) + describe('runtime file discovery', () => { test('files added at runtime inside an existing extension trigger file_created', async () => { await inTemporaryDirectory(async (dir) => { diff --git a/packages/app/src/cli/services/dev/app-events/file-watcher.ts b/packages/app/src/cli/services/dev/app-events/file-watcher.ts index 4a9f48ab75a..4448616e4fd 100644 --- a/packages/app/src/cli/services/dev/app-events/file-watcher.ts +++ b/packages/app/src/cli/services/dev/app-events/file-watcher.ts @@ -35,8 +35,6 @@ const ALWAYS_IGNORED_PATHS = [ '**/*~', ] -const CARGO_BUILD_DIRECTORIES = ['debug', 'release', 'wasm32-wasip1', 'wasm32-wasi', 'rust-analyzer'] - /** * Event emitted by the file watcher * @@ -74,6 +72,7 @@ export interface OutputContextOptions { export class FileWatcher { private currentEvents: WatcherEvent[] = [] private extensionPaths: string[] = [] + private functionOutputDirectories: string[] = [] private app: AppLinkedInterface private readonly options: OutputContextOptions private onChangeCallback?: (events: WatcherEvent[]) => void @@ -134,22 +133,22 @@ export class FileWatcher { const allWatchedFiles = this.getAllWatchedFiles() watchPaths.push(...allWatchedFiles) - // Anchor Cargo output patterns to Rust function roots so source and imported - // paths such as src/target/debug remain watched. - const cargoIgnoredPaths = this.app.realExtensions - .filter( - (extension) => extension.isFunctionExtension && fileExistsSync(joinPath(extension.directory, 'Cargo.toml')), - ) - .flatMap((extension) => - CARGO_BUILD_DIRECTORIES.map((directory) => joinPath(extension.directory, 'target', directory, '**')), - ) - this.close() // Create new watcher const {default: chokidar} = await import('chokidar') + const cargoProjects = new Map() this.watcher = chokidar.watch(watchPaths, { - ignored: [...ALWAYS_IGNORED_PATHS, ...cargoIgnoredPaths], + ignored: [ + ...ALWAYS_IGNORED_PATHS, + (path: string) => { + const normalizedPath = normalizePath(path) + const isFunctionOutput = this.functionOutputDirectories.some( + (directory) => normalizedPath === directory || normalizedPath.startsWith(`${directory}/`), + ) + return isFunctionOutput || isInCargoTargetDirectory(normalizedPath, cargoProjects) + }, + ], persistent: true, ignoreInitial: true, }) @@ -162,6 +161,9 @@ export class FileWatcher { updateApp(app: AppLinkedInterface) { this.app = app + this.functionOutputDirectories = app.nonConfigExtensions + .filter((extension) => extension.isFunctionExtension) + .map((extension) => normalizePath(joinPath(extension.directory, dirname(extension.outputRelativePath)))) this.extensionPaths = this.app.nonConfigExtensions .map((ext) => normalizePath(ext.directory)) .filter((dir) => dir !== this.app.directory) @@ -460,3 +462,22 @@ export class FileWatcher { return ignore.default().add(gitIgnoreContent) } } + +/** + * Match Cargo output even when a descendant is supplied as an explicit watch path. + * A sibling Cargo.toml distinguishes build output from source folders named target. + */ +function isInCargoTargetDirectory(path: string, cargoProjects: Map): boolean { + const segments = path.split('/') + for (let index = segments.length - 1; index > 0; index--) { + if (segments[index] !== 'target') continue + const projectDirectory = segments.slice(0, index).join('/') + let isCargoProject = cargoProjects.get(projectDirectory) + if (isCargoProject === undefined) { + isCargoProject = fileExistsSync(joinPath(projectDirectory, 'Cargo.toml')) + cargoProjects.set(projectDirectory, isCargoProject) + } + if (isCargoProject) return true + } + return false +} From bdb2926fbceda04fb278c9656c7b23e79486a683 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isaac=20Rold=C3=A1n?= Date: Fri, 23 Oct 2026 11:29:46 +0200 Subject: [PATCH 3/3] Read current app extensions in the build output matcher --- .../src/cli/services/dev/app-events/file-watcher.ts | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/packages/app/src/cli/services/dev/app-events/file-watcher.ts b/packages/app/src/cli/services/dev/app-events/file-watcher.ts index 4448616e4fd..89a7a12edee 100644 --- a/packages/app/src/cli/services/dev/app-events/file-watcher.ts +++ b/packages/app/src/cli/services/dev/app-events/file-watcher.ts @@ -72,7 +72,6 @@ export interface OutputContextOptions { export class FileWatcher { private currentEvents: WatcherEvent[] = [] private extensionPaths: string[] = [] - private functionOutputDirectories: string[] = [] private app: AppLinkedInterface private readonly options: OutputContextOptions private onChangeCallback?: (events: WatcherEvent[]) => void @@ -143,9 +142,11 @@ export class FileWatcher { ...ALWAYS_IGNORED_PATHS, (path: string) => { const normalizedPath = normalizePath(path) - const isFunctionOutput = this.functionOutputDirectories.some( - (directory) => normalizedPath === directory || normalizedPath.startsWith(`${directory}/`), - ) + const isFunctionOutput = this.app.realExtensions.some((extension) => { + if (!extension.isFunctionExtension) return false + const directory = normalizePath(joinPath(extension.directory, dirname(extension.outputRelativePath))) + return normalizedPath === directory || normalizedPath.startsWith(`${directory}/`) + }) return isFunctionOutput || isInCargoTargetDirectory(normalizedPath, cargoProjects) }, ], @@ -161,9 +162,6 @@ export class FileWatcher { updateApp(app: AppLinkedInterface) { this.app = app - this.functionOutputDirectories = app.nonConfigExtensions - .filter((extension) => extension.isFunctionExtension) - .map((extension) => normalizePath(joinPath(extension.directory, dirname(extension.outputRelativePath)))) this.extensionPaths = this.app.nonConfigExtensions .map((ext) => normalizePath(ext.directory)) .filter((dir) => dir !== this.app.directory)