Skip to content

Watch asset directories for changes during dev#7470

Open
melissaluu wants to merge 1 commit intomainfrom
05-05-add-directories-to-watched-files
Open

Watch asset directories for changes during dev#7470
melissaluu wants to merge 1 commit intomainfrom
05-05-add-directories-to-watched-files

Conversation

@melissaluu
Copy link
Copy Markdown
Contributor

@melissaluu melissaluu commented May 5, 2026

WHY are these changes introduced?

Closes https://github.com/shop/issues-admin-extensibility/issues/2461

When developers add new files to the watched assets directories during a dev sesstion, they were not being picked up by the file watcher and meant that adding an asset image would not trigger a rebuild or hot-reload.

WHAT is this pull request doing?

Introduces a mechanism for ExtensionInstance to track additional watched paths beyond its source imports, and wires it into the file watcher and build pipeline:

  • Adds addWatchedPath, getWatchedPaths, and clearWatchedPaths methods to ExtensionInstance. Paths are stored in a deduplicated Set and are included in the output of watchedFiles().
  • In executeIncludeAssetsStep, directory-type asset sources are registered as watched paths on the extension after being copied, so the watcher knows to monitor them.
  • In FileWatcher, the extensionWatchedFiles map now tracks both files and directories. A new getExtensionHandlesForFilePath method resolves the owning extension for any file path by checking both exact matches and prefix matches against watched directories. Directories themselves are excluded from the paths passed to chokidar (since chokidar already watches the extension directory), but files inside them are still attributed to the correct extension.
  • In AppEventWatcher, after building extensions, the set of watched paths is compared before and after the build. If new paths were registered (e.g., a new asset directory was discovered), the file watcher is restarted to pick them up.

How to test your changes?

Video

  1. Create an app-home extension that uses include_assets with a asset config key pointing to a directory.
  2. Run shopify app dev.
  3. Add a file inside the watched asset directory.
  4. Confirm that the change is detected as a new websocket message and triggers the expected rebuild/update.
  5. Update the app to show the new image and verify the image is displayed correctly
  6. Rename the new file and ensure the app now shows a broken image
  7. Rename the file back to the original name and check if the app now shows the correct image again
  8. Delete the file and check if the app shows the broken image

Checklist

  • I've considered possible cross-platform impacts (Mac, Linux, Windows)
  • I've considered possible documentation changes
  • I've considered analytics changes to measure impact
  • The change is user-facing — I've identified the correct bump type (patch for bug fixes · minor for new features · major for breaking changes) and added a changeset with pnpm changeset addgeeck

Copy link
Copy Markdown
Contributor Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@github-actions github-actions Bot added the no-changelog This PR doesn't include a changeset entry. Is an internal only change not relevant to end users. label May 5, 2026
@melissaluu melissaluu force-pushed the 05-05-add-directories-to-watched-files branch 4 times, most recently from 5f3d9d6 to 92ac699 Compare May 5, 2026 22:05
@melissaluu melissaluu changed the title add-directories-to-watched-files Watch asset directories for changes during dev May 5, 2026
@melissaluu melissaluu force-pushed the 05-05-add-directories-to-watched-files branch from 92ac699 to 4034f2f Compare May 5, 2026 22:11
@melissaluu melissaluu marked this pull request as ready for review May 5, 2026 22:11
Copilot AI review requested due to automatic review settings May 5, 2026 22:11
@melissaluu melissaluu requested a review from a team as a code owner May 5, 2026 22:11
@melissaluu melissaluu requested review from alfonso-noriega, Copilot, isaacroldan and vividviolet and removed request for Copilot May 5, 2026 22:12
@melissaluu melissaluu force-pushed the 05-05-add-directories-to-watched-files branch from 4034f2f to 3feb8ae Compare May 6, 2026 16:25
@melissaluu melissaluu force-pushed the 05-05-add-directories-to-watched-files branch from 3feb8ae to 8e21010 Compare May 6, 2026 21:12
// For deleted files, skip the live watchPaths check — the file is already gone from disk
// so globSync won't find it. The file was already validated as belonging to the extension
// via the cached extensionWatchedFiles map earlier in the flow.
if (event.type === 'file_deleted') return false
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added this to enable deleting an asset will emit a websocket update event.

Without it, since the shouldIgnoreEvent() calls extension.watchedFiles() which runs globSync live against the filesystem. For delete events, by the time this check runs, the file is already gone from disk — so globSync never returns it, isAValidWatchedPath is false, and the event gets silently dropped.

Copy link
Copy Markdown
Contributor Author

/snapit

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 6, 2026

🫰✨ Thanks @melissaluu! Your snapshot has been published to npm.

Test the snapshot by installing your package globally:

pnpm i -g --@shopify:registry=https://registry.npmjs.org @shopify/cli@0.0.0-snapshot-20260506213108

Caution

After installing, validate the version by running shopify version in your terminal.
If the versions don't match, you might have multiple global instances installed.
Use which shopify to find out which one you are running and uninstall it.

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

Labels

no-changelog This PR doesn't include a changeset entry. Is an internal only change not relevant to end users.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant