Skip to content
This repository has been archived by the owner on Jun 1, 2023. It is now read-only.

Block directories in theme assets #1934

Merged
merged 6 commits into from
Mar 22, 2022
Merged

Conversation

amcaplan
Copy link
Contributor

WHY are these changes introduced?

This is the followup to #1881. We were failing in an unexpected way because we assumed users would not have subdirectories in their asset directories, rather than enforcing this behavior. @macournoyer graciously confirmed that asset subdirectories are not supported. So we're going to fail with a proper validation message.

WHAT is this pull request doing?

Before: Bugsnag

Screen Shot 2022-01-16 at 21 48 22

After:

Screen Shot 2022-01-16 at 21 49 19

Note the small additional benefit that following this PR, we're validating earlier in the process, preventing the frustration of syncing to 100% and only then discovering things are broken.

How to test your changes?

Create a theme with a subdirectory within assets/ and try to shopify theme serve

Update checklist

  • I've added a CHANGELOG entry for this PR (if the change is public-facing)
  • I've considered possible cross-platform impacts (Mac, Linux, Windows).
  • I've left the version number as is (we'll handle incrementing this when releasing).

@amcaplan amcaplan requested review from macournoyer, a team, pepicrft and gonzaloriestra and removed request for a team January 16, 2022 19:51
Comment on lines 117 to 120
invalid_subdirectory: "The directory %s is in an invalid location."\
" Nonstandard subdirectories are not supported within themes."\
" For more details on supported theme directory structure, see"\
" https://shopify.dev/themes/architecture#directory-structure-and-component-types."
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@MeredithCastile feel free to rewrite. The point is that the user has created a subdirectory in a location where our supported directory structure doesn't allow it (and apparently this is a hard requirement on our backend).

Copy link
Contributor

Choose a reason for hiding this comment

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

@amcaplan What's the task the user is trying to do when they'd hit this message? Adding assets to the subdirectory, right?

First draft pending your answer: "Assets can't be added to subdirectories. Add them directly to your assets folder (/assets)."

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@MeredithCastile have a look at the PR message images, it happens when attempting to serve the theme. So they wouldn't necessarily have just added this file.

There's also the general point that while we see this in assets mostly (presumably users want to have more organization around their assets), technically we could see failures in other areas as well, if the user has violated the officially supported directory structure.

Copy link
Contributor

Choose a reason for hiding this comment

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

@amcaplan Ah, I understand now. Thanks for talking me through this earlier. Let's do this:

image

Your directory structure isn't supported.

Move any files to a parent folder, then delete unsupported subdirectories.

Required directory structure: https://shopify.dev/themes/architecture#directory-structure-and-component-types

Nice to have would be the indentation and bullet before the doc / link (as in the Figma screenshot).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Slight adjustment so they know what to delete:

Screen Shot 2022-03-22 at 20 42 00

Copy link
Contributor

@macournoyer macournoyer left a comment

Choose a reason for hiding this comment

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

That's awesome! So much better error message now. Thanks Ariel 🙏

lib/shopify_cli/theme/theme.rb Outdated Show resolved Hide resolved
Copy link
Contributor

@macournoyer macournoyer left a comment

Choose a reason for hiding this comment

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

EDIT: whoops, github hiccup... sorry for the double post

Comment on lines 39 to 44
paths = root.glob(pattern)
if raise_on_dir
paths.each do |path|
if ::File.directory?(path)
@ctx.abort(@ctx.message('theme.serve.error.invalid_subdirectory', path.to_s))
end
end
end
paths.map { |path| File.new(path, root) }
end
Copy link
Contributor

Choose a reason for hiding this comment

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

What about following this other approach? I think it's a bit more readable and it just loops through each file once.

Suggested change
paths = root.glob(pattern)
if raise_on_dir
paths.each do |path|
if ::File.directory?(path)
@ctx.abort(@ctx.message('theme.serve.error.invalid_subdirectory', path.to_s))
end
end
end
paths.map { |path| File.new(path, root) }
end
root.glob(pattern).map do |path|
abort_if_directory(path) if raise_on_dir
File.new(path, root)
end
def abort_if_directory(path)
return unless ::File.directory?(path)
@ctx.abort(@ctx.message('theme.serve.error.invalid_subdirectory', path.to_s))
end

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Love it, thanks for the suggestion @gonzaloriestra!

@amcaplan amcaplan force-pushed the block-directories-in-theme-assets branch from a70e7bc to b32908a Compare January 24, 2022 09:31
@github-actions
Copy link

This PR seems inactive. If it's still relevant, please add a comment saying so. Otherwise, take no action.

→ If there's no activity within a week, then a bot will automatically close this.

Thanks for helping to improve Shopify's dev tooling and experience.

@amcaplan amcaplan requested a review from a team March 22, 2022 18:45
@amcaplan amcaplan force-pushed the block-directories-in-theme-assets branch from 315b6e3 to 4ef218e Compare March 22, 2022 18:52
@amcaplan amcaplan merged commit 05bdeec into main Mar 22, 2022
@amcaplan amcaplan deleted the block-directories-in-theme-assets branch March 22, 2022 19:45
@shopify-shipit shopify-shipit bot temporarily deployed to rubygems March 24, 2022 11:32 Inactive
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants