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

Manually add .js extension to relative imports #1455

Merged
merged 6 commits into from
May 7, 2024

Conversation

Intizar-T
Copy link
Contributor

@Intizar-T Intizar-T commented May 6, 2024

Closes #1454

Description

We are using ES modules in the cli workspace and currently, most if not all of our imports in the cli workspace don't include the file extension, which is a requirement in ES modules. Since tsc doesn't add .js extension during compilation, there is a module not found error when we try to run node dist/index.js.

This PR:

  • manually adds .js extensions to all imports
  • adds an eslint-plugin-import package to provide linting error suggestions when imported file extension is not provided (ignores the test dir)

I've tested running the cli executable after these implementations and the module not found error has been resolved.

Fixes # (issue)

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Checklist before requesting a review

  • I have performed a self-review of my code.
  • If it is a core feature, I have added thorough tests.

Deployment

  • Should publish npm package
  • Should publish Docker image

@Intizar-T Intizar-T added invalid This doesn't seem right cli labels May 6, 2024
@Intizar-T Intizar-T self-assigned this May 6, 2024
Copy link
Contributor

coderabbitai bot commented May 6, 2024

Walkthrough

Walkthrough

The modifications across various files in the CLI workspace primarily involve updating import statements to include .js extensions explicitly. This change ensures that the TypeScript compiled JavaScript files are compatible with Node.js module resolution without requiring additional runtime flags.

Changes

File Pattern Change Summary
cli/src/*.ts Updated import paths to include .js extensions for all internal module imports.
.vscode/extensions.json Added recommendations for VS Code extensions eslint and prettier.
cli/.eslintrc Added "import" plugin and configured "import/extensions" rule.

Assessment against linked issues

Objective Addressed Explanation
Resolve module resolution issue in CLI workspace (#1454)
Ensure node compatibility without runtime flags (#1454)

Poem

🐇 Oh hop and tweak, in the code we peek,
🌟 With .js extensions, no more bleak!
A rabbit's joy in each file's link,
Now runs so smooth, faster than you think!
🎉 Celebrate the code, clean and sleek! 🚀
For every import, the right path we seek.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@Intizar-T Intizar-T marked this pull request as ready for review May 6, 2024 13:29
@Intizar-T Intizar-T requested a review from a team as a code owner May 6, 2024 13:29
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Out of diff range and nitpick comments (3)
cli/src/listener.ts (1)

Line range hint 129-129: Specify a more appropriate type instead of any.

Using any type can lead to potential runtime errors due to lack of type safety. Consider specifying a more detailed type for printResult.

- const printResult: any[] = []
+ const printResult: Array<{ [key: string]: string }> = []
cli/src/reporter.ts (1)

Line range hint 153-153: Specify a more appropriate type instead of any.

To enhance type safety and prevent potential runtime issues, replace the any type with a more specific type.

- const printResult: any[] = []
+ const printResult: Array<{ [key: string]: string }> = []
cli/src/delegator.ts (1)

Line range hint 302-302: Specify a more appropriate type instead of any.

Using any type reduces type safety, which can lead to bugs. It's recommended to use a more specific type to ensure the integrity of the data being handled.

- const printResult: any[] = []
+ const printResult: Array<{ [key: string]: string }> = []
Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 63d69f1 and 72c88b7.
Files ignored due to path filters (2)
  • cli/package.json is excluded by !**/*.json
  • yarn.lock is excluded by !**/*.lock, !**/*.lock
Files selected for processing (16)
  • .vscode/extensions.json (1 hunks)
  • cli/.eslintrc (1 hunks)
  • cli/src/adapter.ts (1 hunks)
  • cli/src/aggregator.ts (1 hunks)
  • cli/src/chain.ts (1 hunks)
  • cli/src/cli-types.ts (1 hunks)
  • cli/src/datafeed.ts (3 hunks)
  • cli/src/delegator.ts (1 hunks)
  • cli/src/fetcher.ts (1 hunks)
  • cli/src/index.ts (1 hunks)
  • cli/src/listener.ts (1 hunks)
  • cli/src/proxy.ts (1 hunks)
  • cli/src/reporter.ts (1 hunks)
  • cli/src/service.ts (1 hunks)
  • cli/src/utils.ts (1 hunks)
  • cli/src/vrf.ts (1 hunks)
Files skipped from review due to trivial changes (12)
  • .vscode/extensions.json
  • cli/src/adapter.ts
  • cli/src/aggregator.ts
  • cli/src/chain.ts
  • cli/src/cli-types.ts
  • cli/src/datafeed.ts
  • cli/src/fetcher.ts
  • cli/src/index.ts
  • cli/src/proxy.ts
  • cli/src/service.ts
  • cli/src/utils.ts
  • cli/src/vrf.ts
Additional Context Used
GitHub Check Runs (1)
build-publish success (5)

cli/src/delegator.ts: [warning] 302-302:
Unexpected any. Specify a different type


cli/src/listener.ts: [warning] 129-129:
Unexpected any. Specify a different type


cli/src/reporter.ts: [warning] 153-153:
Unexpected any. Specify a different type

Additional comments not posted (6)
cli/.eslintrc (3)

12-13: Addition of @typescript-eslint and import plugins to ESLint configuration.

This ensures TypeScript-specific linting and import rules are enforced, improving code quality and consistency.


20-28: Configuration of TypeScript-specific and import extension rules.

Disabling TypeScript-specific lint warnings (@typescript-eslint/ban-ts-comment) and enforcing import extensions for .js and .ts files enhances code reliability and module resolution correctness.


30-37: Override settings for test directories to disable import extension rules.

This is a sensible exception, as test environments often have different module resolution configurations. It prevents unnecessary linting errors in tests.

cli/src/listener.ts (1)

3-11: Update of import statements to include .js extensions.

This change is crucial for correct module resolution in Node.js environments when using ES modules, aligning with the PR's objectives.

cli/src/reporter.ts (1)

10-12: Update of import statements to include .js extensions.

Ensures that the module resolution works correctly in a Node.js environment, which is critical for the functionality of the CLI tool.

cli/src/delegator.ts (1)

3-4: Update of import statements to include .js extensions.

Correctly resolves modules in ES module environments, crucial for the operation of the CLI tool.

@nick-bisonai
Copy link
Collaborator

In which situation were node dist/index.js being called?

I haven't been experiencing module not found error in my use case when calling commands through yarn cli {COMMAND}. It'd be nice to remove error but I'm not sure if it is a necessary update if it adds more code with extension specification and bit more configuration while I can't see the error in use cases. I'd be grateful if you can elaborate a bit more why this update is necessary.

@Intizar-T
Copy link
Contributor Author

In which situation were node dist/index.js being called?

I haven't been experiencing module not found error in my use case when calling commands through yarn cli {COMMAND}. It'd be nice to remove error but I'm not sure if it is a necessary update if it adds more code with extension specification and bit more configuration while I can't see the error in use cases. I'd be grateful if you can elaborate a bit more why this update is necessary.

In which situation were node dist/index.js being called?

I haven't been experiencing module not found error in my use case when calling commands through yarn cli {COMMAND}. It'd be nice to remove error but I'm not sure if it is a necessary update if it adds more code with extension specification and bit more configuration while I can't see the error in use cases. I'd be grateful if you can elaborate a bit more why this update is necessary.

thanks for the feedback Nick. in cli/package.json we have

"bin": {
    "cli": "dist/index.js"
  }

we also explain in the documentation that users can call the cli command globally with npx @bisonai/orakl-cli. When you call the cli with yarn, you'll notice that we are using the --import=specifier-resolution-node/register flag which solves this module not found error. However in the bin executable that flag is not passed and it's not that simple to pass flags in the executable (couldn't find an easy workaround). Therefore when you call the cli command using the executable (through npx @bisonai/orakl-cli), you get module not found error. So we either need to update the documentation to remove executable usage or implement a fix so that the executable can be called globally. I explained this in more detail in the tagged issue.

Copy link
Collaborator

@nick-bisonai nick-bisonai left a comment

Choose a reason for hiding this comment

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

lgtm! I didn't know that the document was recommending to use binary file, let's merge it

@Intizar-T Intizar-T merged commit 2268213 into master May 7, 2024
1 check passed
@Intizar-T Intizar-T deleted the i-1454/fix/module-resolution-cli branch May 7, 2024 07:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cli invalid This doesn't seem right
Projects
None yet
Development

Successfully merging this pull request may close these issues.

module resolution issue in cli workspace
2 participants