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

[fix] Process symlinked routes #4957

Merged
merged 5 commits into from
May 23, 2022
Merged

[fix] Process symlinked routes #4957

merged 5 commits into from
May 23, 2022

Conversation

mikenikles
Copy link
Contributor

@mikenikles mikenikles commented May 16, 2022

Fixes #4936.

Process files within sym-linked directories.

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. All changesets should be patch until SvelteKit 1.0

@changeset-bot
Copy link

changeset-bot bot commented May 16, 2022

🦋 Changeset detected

Latest commit: 3980e3c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Member

@mrkishi mrkishi left a comment

Choose a reason for hiding this comment

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

As per @Conduitry's comment, this would error on symlinks to files, as readdirSync throws when not called on a directory.

There seems to be a problem with Node's fs.readdirSync(dir, { withFileTypes: true }) returning false for isDirectory() on symlinks (open issue at nodejs/node#30646). It doesn't seem likely to get fixed upstream in a timely manner, so reverting to fs.statSync instead of withFileTypes seems like a better solution.

@mikenikles
Copy link
Contributor Author

Thank you folks, I reverted to the fs.statSync approach in 801bd67.

@mrkishi
Copy link
Member

mrkishi commented May 18, 2022

Thank you for the follow-up!

What should we do about the test? On Windows, the system's developer mode would have to be enabled as well as Git's core.symlinks, otherwise Git won't recreate the symlinks and it'll fail.

We could add core.symlinks to .gitconfig but that'd still fail when developer mode is disabled. Perhaps it'd be okay to skip it on Windows?

Co-authored-by: Maurício Kishi <mrkishi@users.noreply.github.com>
@mikenikles
Copy link
Contributor Author

Interesting, I see the tests on Windows passed with the latest commit, but failed previously - although I don't think it was because of the new test I added 🤔.

To make sure we have stable tests though, I agree that it's likely ok to skip on Windows. However, can you guide me into how I can exclude my sym-link test in a Windows environment?

@mrkishi mrkishi changed the title [fix] Process sym-linked routes [fix] Process symlinked routes May 22, 2022
Copy link
Member

@mrkishi mrkishi left a comment

Choose a reason for hiding this comment

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

Alright, I pushed changes so tests are skipped when symlinks don't make it through git.

lgtm.

@Rich-Harris
Copy link
Member

thank you!

@oneezy
Copy link

oneezy commented Jun 2, 2022

Hey guys!
I went ahead and wrote a thorough breakdown of how our team got Sveltekit Symlink Routes working on Windows. This probably isn't the best place for it but wanted to share the process (would be cool to eventually see this explained in Kit Docs).

Thanks again @mikenikles for your help !


Windows Symlinks

Prerequisites

Symlink Permissions

You have 1 of 3 options. If you try to create a symlink without one of these, you will see:

Failed to create symbolic link 'link' : Operation not permitted
🟦 Option 1: Run as Administrator

When you make a symlink this way, you'll always need to open up the terminal as an Administrator.

🟦 Option 2: Developer Mode

This is the easiest way but not the safest (security risk).

  1. Go to your start menu and search "Developer Settings".
  2. Turn Developer Mode On.
✅ Option 3: Local Security Policy
  1. Go to your start menu and search "Local Security Policy".
  2. Double click on Create symbolic links
+ Security Settings > Local Policies > User Rights Assignment > Create symbolic links
  1. Click the Add User or Group button
  2. Type in your username and click the OK button
  3. Click the Apply button
  4. Restart your computer for the settings to take place

Choose a Path: Command Prompt, Git Bash or WSL

Command Prompt

  1. Open the Command Prompt
  2. Use cd to go into your project where you want to add the symlink
cd C:\Users\oneezy\Desktop\sveltekit-symlinks\src\routes
  1. Create a relative symlink (not absolute!) to a directory folder (not a file!)
mklink /D my-new-symlink ..\..\symlinked-docs
  1. And your finished!

Git Bash

Step 1. Setting System Environment Variables

🟦 Option 1: Exporting variable per session (tedious)

In your terminal you'll need to write the following command every time you restart git bash.

export MSYS=winsymlinks:nativestrict
✅ Option 2: Create an entry for MSYS (once)
  1. Go to your start menu and search for "System Settings"
  2. Click the Environment Variables... button
  3. Go to System variables section and click the New button
  4. Enter in the New System Variable name and value and click the OK button.
Variable name:    MSYS
Variable value:   winsymlinks:nativestrict
  1. Click the OK button again to confirm.
  2. Congratulations! Now you have the power to create symlinks whenever you want!

Step 2. Enable Symlinks in Gitconfig

You may also need to enable symlinks in your .gitconfig user profile.

git config --global core.symlinks true

This will add the symlinks = true to C:\Users\username\.gitconfig

[core]
  symlinks = true

You may also need to manually add symlinks = true to the gitconfig inside C:\Program Files\Git\etc\gitconfig if it's not there already

[core]
  symlinks = true

Step 3. Create Symlink (Git Bash)

  1. Open up Git Bash
  2. Use cd to go into your project where you want to add the symlink
cd c/users/oneezy/desktop/sveltekit-symlinks/src/routes
  1. Create a relative symlink (not absolute!) to a directory folder (not a file!)
ln -s ../../symlinked-docs my-new-symlink
  1. And your finished!

PROTIP: To see a list of your symlinks use the ls -l command

oneezy@oneezy MINGW64 ~/Desktop/sveltekit-symlinks/src/routes (main)
$ ls -l

rw-r--r-- 1 oneezy 197609 1354 Jun  1 13:00 index.svelte
+lrwxrwxrwx 1 oneezy 197609   20 Jun  1 20:45 my-new-symlink -> ../../symlinked-docs/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1.0.0-next.306 broke Vite's preserveSymlinks
5 participants