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: Correct template path resolution #25

Merged
merged 5 commits into from
Jan 6, 2025

Conversation

damassi
Copy link
Contributor

@damassi damassi commented Jan 6, 2025

User description

Taking the lib for a spin, I noticed that it wasn't resolving the paths to my preexisting templates via the register command. It would come out like:

supabase/migrations-templates/Users/cn/Sites/cn/my-project/supabase/migrations-templates/checkout.sql

And looking at the input paths this seemed indeed an issue:

path.join(baseDir, config.templateDir, templatePath)

so process.cwd() would append the dir and then the path.

Looking closer, by the time templatePath is input, its already been resolved here: https://github.com/t1mmen/srtd/blob/main/src/lib/templateManager.ts#L82

So this simplifies things and fixes the issue, allowing me to register my templates.


PR Type

Bug fix


Description

  • Fixed incorrect template path resolution in registerTemplate.

  • Removed redundant configuration-based path resolution logic.

  • Simplified path resolution to use path.resolve.


Changes walkthrough 📝

Relevant files
Bug fix
registerTemplate.ts
Simplify and fix template path resolution logic                   

src/utils/registerTemplate.ts

  • Removed redundant getConfig import and usage.
  • Simplified path resolution logic by removing unnecessary paths.
  • Updated to use path.resolve for template path resolution.
  • +1/-5     

    💡 PR-Agent usage: Comment /help "your question" on any pull request to receive relevant information

    Copy link

    changeset-bot bot commented Jan 6, 2025

    🦋 Changeset detected

    Latest commit: 1f7d7f9

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

    This PR includes changesets to release 1 package
    Name Type
    @t1mmen/srtd 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
    Contributor

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 1 🔵⚪⚪⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Path Resolution

    The PR reduces the number of path resolution attempts to a single path. Consider keeping a fallback path resolution strategy to maintain flexibility.

    const pathsToTry = [
      path.resolve(templatePath),
    ];

    Copy link
    Contributor

    qodo-merge-pro-for-open-source bot commented Jan 6, 2025

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    ✅ Enhance path resolution to handle both absolute and relative template paths
    Suggestion Impact:The commit directly implements the suggested path resolution changes by adding path.resolve(baseDir, templatePath) and path.join(baseDir, templatePath) to the pathsToTry array

    code diff:

       const pathsToTry = [
         path.resolve(templatePath),
         path.resolve(baseDir, templatePath),
    -    path.join(baseDir, templatePath)
    +    path.join(baseDir, templatePath),
       ];

    Add additional path resolution options to handle relative paths from baseDir.
    Currently, only absolute path resolution is attempted, which could fail for relative
    template paths.

    src/utils/registerTemplate.ts [10-12]

     const pathsToTry = [
       path.resolve(templatePath),
    +  path.resolve(baseDir, templatePath),
    +  path.join(baseDir, templatePath)
     ];
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: The suggestion addresses a significant limitation in the PR's path resolution logic. Adding support for both absolute and relative paths is crucial for proper template path handling, especially since the PR removed the previous template directory resolution.

    8
    General
    Add input validation to prevent processing of invalid path parameters

    Add input validation to ensure templatePath is not empty or undefined before
    attempting path resolution.

    src/utils/registerTemplate.ts [9-12]

     export async function registerTemplate(templatePath: string, baseDir: string): Promise<void> {
    +  if (!templatePath) {
    +    throw new Error('Template path cannot be empty');
    +  }
       const pathsToTry = [
         path.resolve(templatePath),
       ];
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: Input validation is important for robust error handling and preventing potential runtime errors. The suggestion adds a necessary check that would fail fast with a clear error message rather than potentially causing issues during path resolution.

    7

    @damassi damassi force-pushed the fix/template-path-resolution branch from 8f5da13 to 4cf0240 Compare January 6, 2025 06:43
    @damassi damassi force-pushed the fix/template-path-resolution branch from 4cf0240 to 79b7d11 Compare January 6, 2025 06:45
    @t1mmen
    Copy link
    Owner

    t1mmen commented Jan 6, 2025

    Ops, sorry about that. Thanks for fixing, @damassi 🙏

    I'll take a closer look and ship this fix later today.

    t1mmen added a commit that referenced this pull request Jan 6, 2025
    @t1mmen
    Copy link
    Owner

    t1mmen commented Jan 6, 2025

    I made some improvements to path detection in general (so srtd can run from any directory in the project).

    I had to move your changes into #main, to fix some biome/formatting issues.

    Edit: Releases as 0.4.6, hopefully that works for you!

    Thanks again for contributing, @damassi 🙏

    @t1mmen t1mmen merged commit aaa78e7 into t1mmen:main Jan 6, 2025
    @damassi
    Copy link
    Contributor Author

    damassi commented Jan 6, 2025

    Thank you for the quick fix! This tool is seriously the tool that SQL has needed for a long time. The possibilities are 🤯

    @damassi damassi deleted the fix/template-path-resolution branch January 6, 2025 15:03
    @damassi
    Copy link
    Contributor Author

    damassi commented Jan 6, 2025

    Also @t1mmen - sorry for missing the formatting fixes; the issue I ran into requiring the --no-verify is that I don't use nvm to manage node versions, but rather asdf, so couldn't run the tool out of the box (even though I do use biome). So might want to make that contributor flow a bit more general.

    @t1mmen
    Copy link
    Owner

    t1mmen commented Jan 6, 2025

    This tool is seriously the tool that SQL has needed for a long time. The possibilities are 🤯

    @damassi I'm slightly relieved this is the feedback I'm getting :D I sort of couldn't believe how ancient the DX around Postgres was in this day and age, compared to JS/TS-world. Surely I was missing something, I thought. Something like srtd had to exist, I figured... yet, I couldn't find it, hence this project :)

    As for simplifying contributions, I'll probably re-visit that if issues/PR's keep coming. I wasn't really expecting anyone to notice this/care to fix things, but glad to be proven wrong :)

    @damassi
    Copy link
    Contributor Author

    damassi commented Jan 6, 2025

    Yes, for example:

    • All of our core evolving sql code now lives in src/sql -- with the rest of our source code!
    • srtd watches src/sql/*, and inside there things are broken up into obvious files/categories one would expect
    • folders such as sql/functions, sql/types, sql/rls, etc
    • contributors now know exactly where to look for things (though obviously, one must still remain vigilant with postgres)

    Would love to see this somehow integrated into the supabase cli itself 😉

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

    Successfully merging this pull request may close these issues.

    2 participants