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

Layout relative path problem with monorepo and VSCode #556

Open
Tracked by #584
Magnus-Ignitic opened this issue Dec 15, 2023 · 3 comments
Open
Tracked by #584

Layout relative path problem with monorepo and VSCode #556

Magnus-Ignitic opened this issue Dec 15, 2023 · 3 comments
Labels
assigned Whether or not this bug has been assigned some to some other issues as a subtask or pre-req environment An issue with the environment
Milestone

Comments

@Magnus-Ignitic
Copy link

Magnus-Ignitic commented Dec 15, 2023

Summary

When adding mdsvex to a mono repo SvelteKit project (where the SvelteKit project resides in a subfolder) I get the following error in source files:

Error: The layout path you provided couldn't be found at either
./src/mdsvex.svelte or /[Workspace root path]/src/mdsvex.svelte.
Please double-check it and try again.

It seems the relative path is based on the workspace root folder and not the location of svelte.config.js.

I only get the error in VSCode. Dev server and building works fine.

svelte.config.js

import adapter from '@sveltejs/adapter-netlify';
import { vitePreprocess } from '@sveltejs/kit/vite';
import path from 'path';
import { mdsvex } from 'mdsvex';

/** @type {import('mdsvex').MdsvexOptions} */
const mdsvexOptions = {
  extensions: ['.md'],
  layout: {
    _: './src/mdsvex.svelte' // ← Also tried with path.resolve('./src/mdsvex.svelte')
  }
}

/** @type {import('@sveltejs/kit').Config} */
const config = {
  // Consult https://kit.svelte.dev/docs/integrations#preprocessors
  // for more information about preprocessors
  extensions: ['.svelte', '.md'],
  preprocess: [mdsvex(mdsvexOptions), vitePreprocess({})],
  kit: {
    adapter: adapter(),
  // ...
}

Workaround(?)

Using dirname() to get the absolute path to the svelte.config.js seems to work:

const mdsvexOptions = {
  extensions: ['.md'],
  layout: {
    _: dirname(fileURLToPath(import.meta.url)) + '/src/mdsvex.svelte'
  }
}
@joekrump
Copy link

joekrump commented Jan 4, 2024

Thanks for taking the time to write this up @Magnus-Ignitic . I just ran into the same problem and the workaround provided helped me get unstuck.

@pngwn pngwn modified the milestones: 1.0 docs + integrations, 1.0 Feb 23, 2024
@pngwn pngwn added environment An issue with the environment assigned Whether or not this bug has been assigned some to some other issues as a subtask or pre-req labels Feb 23, 2024
@pngwn pngwn mentioned this issue Feb 23, 2024
11 tasks
@rotimi-best
Copy link

For anyone else trying to figure out where to get dirname and fileURLToPath from, here you go:

import { fileURLToPath } from 'node:url';
import { dirname } from 'node:path';

techniq added a commit to techniq/svelte-ux that referenced this issue Jun 22, 2024
techniq added a commit to techniq/layerchart that referenced this issue Jun 23, 2024
techniq added a commit to techniq/layerchart that referenced this issue Jun 24, 2024
techniq added a commit to techniq/layerchart that referenced this issue Jun 24, 2024
techniq added a commit to techniq/layerchart that referenced this issue Jun 27, 2024
* Add workspace level (recursive) `pnpm check`

* Fix VSCode not displaying errors due to monorepo handling of mdsvex layout. Related: pngwn/MDsveX#556

* Cleanup warnings/TS errors on docs components (except GeoDebug)

* Add `chartContext()` to get strongly typed chart context (from LayerCake)

* More TS / a11y improvements

* More TS / a11y improvements

* Fix/workaround remaining TS / a11y improvements in src/lib

* Fix remaining a11y warnings

* Add types to topojson data and other type improvements

* More progress

* Ignore autoType TS error (unsure how to make it happy)

* More type progress (AnyScale, etc)

* More type progress

* More type progress

* More progress (90 to go)

* Rework accessor and use consistently.  More type fixes

* Improve types of create series data generators

* Fix ts warnings on Bars and Columns examples

* Replace `@ts-ignore` with `@ts-expect-error`

* More type fixes/workarounds

* Update Svelte UX with improved MenuOption type

* Hacks to make typescript happy in template

* Allow <Chart {xDomain}> to be `null` or `null[]` to support Brush use cases

* Support array as well as tuple for `null` | `number` domains

* [Brush] Support passing Date for min/max.  Cast `Date[]` xDomain example as `[Date, Date]`

* Add changesets

* Run svelte-check during CI

* Add missing `@types/d3-color` and `@types/d3-path` packages

* [Chart] Improve default value jsdoc comments

* Add changeset

* Fix createDimensionGetter() for non-band scales (ex. <Labels> on Area chart)

* Use pattern for individual bar highlighting

* Tween all histogram examples

* Fix sparkbar tooltip example

* Hide overflow on ForceGraph examples

* Cleanup console.logs
@sdarnell
Copy link

sdarnell commented Aug 3, 2024

I had a similar issue with Jetbrains Rider/Webstorm which ends up running with the current directory being the settings directory of the IDE.
I did wonder whether a better option for MDsvex might be to scan upwards to find the directory containing a package.json or svelte.config.* file (which would at least fix the my scenario), but that may still not solve all cases. It's a shame Sveltekit doesn't pass through the root path of the project or the dir of the svelte.config file.

Also, just as a note, there's a newer import.meta.dirname https://nodejs.org/api/esm.html#importmetadirname which is a little simpler than fileURLToPath+dirname, e.g.:

_: join(import.meta.dirname, './src/lib/mdsvex/mdsvex.svelte')

But it requires node 20.11.0+

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
assigned Whether or not this bug has been assigned some to some other issues as a subtask or pre-req environment An issue with the environment
Projects
None yet
Development

No branches or pull requests

5 participants