FredKSchott / snowpack

ESM-powered frontend build tool. Instant, lightweight, unbundled development. ✌️
https://www.snowpack.dev
MIT License
19.48k stars 922 forks source link

[BUG] Relative imports '.' and '..' do not work #2829

Closed BPScott closed 3 years ago

BPScott commented 3 years ago

Bug Report Quick Checklist

Describe the bug

Extracted from https://github.com/snowpackjs/snowpack/pull/2707#issuecomment-790926970, so it doesn't get lost.

I've found an issue with relative paths imports - import {BLAH} from '.' and import {BLAH} from '..' do not work as expected, however import {BLAH} from './' and import {BLAH} from '../'do work correctly.

It seems this was a problem in 3.0.x too, however 3.0.x compiled and served the file with incorrect content and the error occured at runtime. However in the 3.1.0-pre.11 the file with problematic code now returns a 500 error, with an unhelpful error message.

Reproduction is available here.

Given the input (found in this file)

import {value1} from '.';
import {value2} from '..';

import {value3} from './';
import {value4} from '../';

In 3.0.x it compiles to the incorrect output:

import {value1} from '.';
import {value2} from '..';

import {value3} from './index.js';
import {value4} from '../index.js';

In 3.1.0-pre.11 it errors during compile:

Error: Package(s) failed to build: Cannot find module '..' from '/Users/ben/src/github.com/BPScott/snowpack-test/bare-relative-import/package.json'

To Reproduce

Reproduction is available here.

Clone that repo, move into the bare-relative-import subfolder and run npm install and npm start

Expected behavior

I would expect the code:

import {value1} from '.';
import {value2} from '..';

import {value3} from './';
import {value4} from '../';

to compile to:

import {value1} from './index.js';
import {value2} from '../index.js';

import {value3} from './index.js';
import {value4} from '../index.js';
FredKSchott commented 3 years ago

Thanks @BPScott, PR opened: https://github.com/snowpackjs/snowpack/pull/2877