Skip to content

[AssetMapper] Bug fix for paths that start with the same string #50355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

weaverryan
Copy link
Member

@weaverryan weaverryan commented May 17, 2023

Q A
Branch? 6.3
Bug fix? yes
New feature? no
Deprecations? no
Tickets None
License MIT
Doc PR Still TODO

Hi!

This should be minor:

  1. Fixed a bug where if 1 asset path directory is a substring of another (e.g. assets/dir and assets/dir2), when you called AssetMapperRepository::findLogicalPath() passing a filesystem path pointed at assets/dir2, it would incorrectly match something in assets/dir.

2) Allowed importmap.php paths to be absolute. The use-case is for UX libraries. UX libraries WILL automatically add a "namespaced path" to the asset mapper. For example, the StimulusBundle I'm working on will likely expose a symfony/stimulus-bundle asset mapper namespace that points to the assets/ directory of that bundle. But, for users, I don't want this to be "another thing you need to know". So, the result of this PR is that, in the user's importmap.php:

    '@symfony/stimulus-bundle' => [
-        // "symfony/stimulus-bundle" is an asset path that points into that bundle
-        'path' => 'symfony/stimulus-bundle/loader.js',
+        'path' => __DIR__.'/../../assets/loader.js',
    ],

The user points to the absolute file, WE figure out which "asset path" that's in, and everything works nicely.

UPDATE: I've removed this.

Cheers!

@@ -76,7 +76,7 @@ public function findLogicalPath(string $filesystemPath): ?string
}

foreach ($this->getDirectories() as $path => $namespace) {
if (!str_starts_with($filesystemPath, $path)) {
if (!str_starts_with($filesystemPath, $path.'/')) {
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

I think we're ok. These are all comparing filesystem paths - not the actual paths that are exposed to the user / importmap.

@weaverryan weaverryan force-pushed the asset-mapper-import-map-absolute-paths branch from 9f35611 to 9158f05 Compare May 19, 2023 00:05
@weaverryan weaverryan changed the title [AssetMapper] Bug fix + allow import map absolute paths [AssetMapper] Bug fix for paths that start with the same string May 19, 2023
@carsonbot carsonbot changed the title [AssetMapper] Bug fix for paths that start with the same string Bug fix for paths that start with the same string May 19, 2023
@weaverryan
Copy link
Member Author

I've just simplified this - it's now just a simple bug fix. The "absolute path" thing might be cool. But in practice, during a UX bundle install, we'll use importmap:require to update the importmap.php using #50363 and there was no graceful way, anyways, for that feature to output a string like __DIR__.'/...', which is what I wanted. So, let's just stick to using asset logical paths everywhere.

@carsonbot carsonbot changed the title Bug fix for paths that start with the same string [AssetMapper] Bug fix for paths that start with the same string May 19, 2023
@nicolas-grekas
Copy link
Member

Thank you @weaverryan.

nicolas-grekas added a commit that referenced this pull request May 19, 2023
…yan)

This PR was squashed before being merged into the 6.3 branch.

Discussion
----------

Bug fix for paths that start with the same string

| Q             | A
| ------------- | ---
| Branch?       | 6.3
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | None
| License       | MIT
| Doc PR        | Still TODO

Hi!

This should be minor:

1) Fixed a bug where if 1 asset path directory is a substring of another (e.g. `assets/dir` and `assets/dir2`), when you called `AssetMapperRepository::findLogicalPath()` passing a filesystem path pointed at `assets/dir2`, it would incorrectly match something in `assets/dir`.

~~2) Allowed `importmap.php` paths to be absolute. The use-case is for UX libraries. UX libraries WILL automatically add a "namespaced path" to the asset mapper. For example, the StimulusBundle I'm working on will likely expose a `symfony/stimulus-bundle` asset mapper namespace that points to the `assets/` directory of that bundle. But, for users, I don't want this to be "another thing you need to know". So, the result of this PR is that, in the user's `importmap.php`:~~

```diff
    '`@symfony`/stimulus-bundle' => [
-        // "symfony/stimulus-bundle" is an asset path that points into that bundle
-        'path' => 'symfony/stimulus-bundle/loader.js',
+        'path' => __DIR__.'/../../assets/loader.js',
    ],
```

~~The user points to the absolute file, WE figure out which "asset path" that's in, and everything works nicely.~~

UPDATE: I've removed this.

Cheers!

Commits
-------

51b9246 Bug fix for paths that start with the same string
@nicolas-grekas nicolas-grekas force-pushed the asset-mapper-import-map-absolute-paths branch from 97a03c2 to 51b9246 Compare May 19, 2023 06:23
@weaverryan weaverryan deleted the asset-mapper-import-map-absolute-paths branch May 19, 2023 10:15
@fabpot fabpot mentioned this pull request May 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants