Skip to content
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

Installed icon packs are not detected in a monorepo setup #187

Open
lukasgreussing opened this issue Jan 6, 2024 · 6 comments
Open

Installed icon packs are not detected in a monorepo setup #187

lukasgreussing opened this issue Jan 6, 2024 · 6 comments
Labels
bug Something isn't working

Comments

@lukasgreussing
Copy link

What version of astro-icon are you using?

v1.0.2

Astro Info

Astro                    v4.1.1
Node                     v20.10.0
System                   Windows (x64)
Package Manager          npm
Output                   static
Adapter                  none
Integrations             none

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

We've encountered an issue where icon packs are not being detected in a monorepo setup, particularly when there's only a single package.json in the root directory.
It appears that the issue may be connected to the changes made in (#175), which might have altered the way icon packs are detected in specific project setups.

Screenshot 2024-01-06 144041

Unfortunately the minimal reproduction example is not working, becauce I'm unable to install the platform dependencies on stackblitz required by nx but the monorepo structure should still be recognizable.

What's the expected result?

Installed icon packs should be detected correctly in a monorepo setup, regardless of the location of the package.json file.

Link to Minimal Reproducible Example

https://stackblitz.com/edit/withastro-astro-q4a1bw?file=apps%2Fplanetary-proxima%2Fsrc%2Fpages%2Findex.astro&view=editor

@natemoo-re natemoo-re added the bug Something isn't working label Jan 8, 2024
@natemoo-re
Copy link
Owner

This is totally valid! I think we'll have to use something like escalade to find the actual root of the project rather than assuming package.json exists in the current directory.

@robin-alphasophia
Copy link

Has anything happened here? We have the same problem - it prevents us from upgrading to the new astro and astro-image version.

Alterantively, if solving this is too hard / time consuming, would it make sense to just add a config option to the icon() function, so that we can point to the folder (does not have to be found automatically, can be set manually once).

@luchillo17
Copy link

luchillo17 commented Mar 26, 2024

I've found this issue is very common in Astro projects, even if I define the root I still have to modify the directories of many integrations with an absolute path based on current file:

// astro.config.mjs -> as it is mjs we use `import.meta.dirname` instead of `__dirname`.
import tailwind from '@astrojs/tailwind';
import vue from '@astrojs/vue';
import icon from 'astro-icon';
import { defineConfig } from 'astro/config';
import { join } from 'node:path';

// https://astro.build/config
export default defineConfig({
  root: import.meta.dirname,
  outDir: '../../dist/apps/portfolio',
  integrations: [
    tailwind({
      configFile: join(import.meta.dirname, './tailwind.config.mjs'),
    }),
    icon({
      iconDir: join(import.meta.dirname, './src/assets/icons'),
    }),
    vue(),
  ],
  i18n: {
    defaultLocale: 'en',
    locales: ['en', 'es'],
  },
});

@mwood23
Copy link

mwood23 commented Mar 30, 2024

Patch for anyone that's interested!

diff --git a/node_modules/astro-icon/dist/loaders/loadIconifyCollections.js b/node_modules/astro-icon/dist/loaders/loadIconifyCollections.js
index dc48df0..060ca03 100644
--- a/node_modules/astro-icon/dist/loaders/loadIconifyCollections.js
+++ b/node_modules/astro-icon/dist/loaders/loadIconifyCollections.js
@@ -45,10 +45,17 @@ export async function loadCollection(name, autoInstall) {
 async function detectInstalledCollections(root) {
     try {
         let packages = [];
-        const text = await readFile(new URL("./package.json", root), {
+        /**
+         * This doesn't recurse up directories to make it work for monorepos that have
+         * all dependencies at the root package.json.
+         *
+         * https://github.com/natemoo-re/astro-icon/issues/187
+         */
+        const text = await readFile(new URL("../../package.json", root), {
             encoding: "utf8",
         });
         const { dependencies = {}, devDependencies = {} } = JSON.parse(text);
+
         packages.push(...Object.keys(dependencies));
         packages.push(...Object.keys(devDependencies));
         const collections = packages

@luchillo17
Copy link

@mwood23 Wouldn't that patch force everyone to use the monorepo folder structure since the location of the package.json would be hardcoded to be 2 folder levels up from the root config file?

@mwood23
Copy link

mwood23 commented Mar 31, 2024

It's just a patch that folks can toss into their repo to unblock using a tool like patch-package. It's not the resolution to the bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants