Skip to content

Bug: context.cwd is incorrect set as / when absolute path is used as filename in rule tester #11274

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

Open
4 tasks done
JounQin opened this issue Jun 5, 2025 · 2 comments
Open
4 tasks done
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working package: rule-tester Issues related to the @typescript-eslint/rule-tester package

Comments

@JounQin
Copy link
Contributor

JounQin commented Jun 5, 2025

Before You File a Bug Report Please Confirm You Have Done The Following...

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have searched for related issues and found none that matched my issue.
  • I have read the FAQ and my problem is not listed.

Relevant Package

rule-tester

Playground Link

No response

Repro Code

basePath = path.parse(
path.resolve(basePath ?? process.cwd(), filename),
).root;

Expected Result

context.cwd should not be set /

Actual Result

context.cwd === '/'

Additional Info

I thought it was ESLint's issue eslint/eslint#19821, and found out it's actually typescript-eslint's.

Versions

package version
@typescript-eslint/rule-tester 8.33.0
@JounQin JounQin added bug Something isn't working triage Waiting for team members to take a look labels Jun 5, 2025
@bradzacher bradzacher added accepting prs Go ahead, send a pull request that resolves this issue package: rule-tester Issues related to the @typescript-eslint/rule-tester package and removed triage Waiting for team members to take a look labels Jun 5, 2025
@nayounsang
Copy link

nayounsang commented Jun 7, 2025

RESOLVED

Did I misunderstand this issue?
When I set absolute path in filename, no problem.

// packages/rule-tester/tests/RuleTester.test.ts

  it('should set weird path for absolute path', () => {
    const absolutePath = path.resolve(__dirname, 'test.ts');
    const tester = new RuleTester();

    tester.run('my-rule', NOOP_RULE, {
      invalid: [],
      valid: [
        {
          code: 'const x = 1;',
          filename: absolutePath,
        },
      ],
    });

    expect(getTestConfigFromCall()).toMatchInlineSnapshot(`
    [
      {
        "code": "const x = 1;",
        "filename": "/",
        "languageOptions": {
          "parserOptions": {
            "disallowAutomaticSingleRunInference": true,
          },
        },
      },
    ]
  `);
  });

test result

expect:     "filename": "/",
received:     "filename": "/Users/***/Desktop/*********/typescript-eslint/packages/rule-tester/tests/test.ts",

If this can be reproduced, this case should pass!

@nayounsang
Copy link

nayounsang commented Jun 7, 2025

resolve my comment: #11274 (comment)
Oh, okay. I found the problematic area and it's reproduced.
For ex)

    it('passes with correct array output', () => {
      expect(() => {
        ruleTester.run('my-rule', rule, {
          invalid: [
            {
              code: 'foo',
              errors: [{ messageId: 'error' }],
              filename: '/test/file.ts',
              output: ['bar', 'baz'],
            },
          ],
          valid: [],
        });
      }).not.toThrow();
    });

In this case, the "/" path really comes out!
There are many other test cases. Analyze them more and add appropriate test cases & fix bugs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working package: rule-tester Issues related to the @typescript-eslint/rule-tester package
Projects
None yet
Development

No branches or pull requests

3 participants