Skip to content

fix: handle OS-level absolute paths in module resolver#30

Merged
ggulpari merged 5 commits intomainfrom
claude/add-greeting-function-01CQtfagG9P8iXVRbbpKLKDp
Nov 17, 2025
Merged

fix: handle OS-level absolute paths in module resolver#30
ggulpari merged 5 commits intomainfrom
claude/add-greeting-function-01CQtfagG9P8iXVRbbpKLKDp

Conversation

@Slashmsu
Copy link
Copy Markdown
Collaborator

The module resolver was incorrectly treating OS-level absolute paths (like /Users/...) as project-relative paths (like /module), causing path duplication when resolving entry points. This fix distinguishes between the two by checking if the absolute path has multiple path segments.

The issue occurred because:

  1. The resolver checked for absolute paths with fs.existsSync()
  2. If the file didn't exist or the check failed, it fell through to the project-relative handler
  3. The project-relative handler (resolveAbsolute) would strip the leading '/' and concatenate with baseUrl, causing duplication

The fix checks if an absolute path has multiple segments (e.g., /Users/foo/bar vs /module) to determine if it's an OS-level absolute path or a project-relative path.

Fixes the issue where 'somon run hello_world.som' would fail with a duplicated path error.

🎉 Pull Request

Description

Type of Change

  • 🐛 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)
  • 📚 Documentation update
  • 🔧 Configuration/build changes
  • ✅ Test improvements
  • ♻️ Code refactoring (no functional changes)

Related Issue

Fixes #(issue number)

Changes Made

Testing

  • All existing tests pass (npm test)
  • Added new tests for the changes
  • Tested manually with examples
  • Linting passes (npm run lint)
  • Type checking passes (npm run type-check)

Checklist

  • My code follows the project's code style guidelines
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings or errors
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published

Additional Context


📋 License Information

SomonScript is open source software licensed under the MIT License.

By submitting this pull request, you agree that your contributions will be
licensed under the MIT License.

For contribution guidelines, please review:


Thank you for contributing to SomonScript! 🚀

@Slashmsu Slashmsu self-assigned this Nov 17, 2025
The module resolver was incorrectly treating OS-level absolute paths (like /Users/...)
as project-relative paths (like /module), causing path duplication when resolving
entry points. This fix distinguishes between the two by checking if the absolute
path has multiple path segments.

The issue occurred because:
1. The resolver checked for absolute paths with fs.existsSync()
2. If the file didn't exist or the check failed, it fell through to the project-relative handler
3. The project-relative handler (resolveAbsolute) would strip the leading '/' and
   concatenate with baseUrl, causing duplication

The fix checks if an absolute path has multiple segments (e.g., /Users/foo/bar vs /module)
to determine if it's an OS-level absolute path or a project-relative path.

Fixes the issue where 'somon run hello_world.som' would fail with a duplicated path error.
@Slashmsu Slashmsu force-pushed the claude/add-greeting-function-01CQtfagG9P8iXVRbbpKLKDp branch from 4a2d877 to a5b5190 Compare November 17, 2025 14:56
@github-actions
Copy link
Copy Markdown

✅ Tests completed on Node.js 20.x: success

1 similar comment
@github-actions
Copy link
Copy Markdown

✅ Tests completed on Node.js 20.x: success

Adds a test that verifies the fix for absolute path resolution in the module
resolver. The test ensures that running 'somon run' with an absolute path
(like /Users/.../hello_world.som) works correctly without path duplication.

The test uses the user's example code:
- Creates a greeting function that takes a name parameter
- Returns a template string with 'Салом' (Hello in Tajik)
- Calls the function and prints the result

This test verifies that the module resolver correctly distinguishes between
OS-level absolute paths (/Users/...) and project-relative paths (/module).
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/module-system/module-resolver.ts Outdated
@github-actions
Copy link
Copy Markdown

✅ Tests completed on Node.js 20.x: success

Bakhtier Gaibulloev added 2 commits November 17, 2025 15:03
The previous implementation incorrectly classified project-relative absolute
imports (like /lib/utils/math.som) as OS-level paths, causing resolution
failures. This commit adds a robust check to distinguish between:

1. OS-level absolute paths: /Users/..., C:\Users\..., /home/..., /tmp/...
2. Project-relative paths: /lib/utils, /module

The new isOsLevelAbsolutePath() method checks:
- If the path starts with the project's baseUrl (OS path within project)
- If the path matches common OS directory patterns (Unix: /home/, /Users/,
  /var/, /tmp/, /opt/, /usr/, /etc/; Windows: C:\, D:\, etc.)
- Otherwise, treats it as project-relative

Added comprehensive tests:
- Project-relative absolute imports with multiple segments (/lib/utils/math)
- OS-level absolute paths (full filesystem paths)
- Ensures both resolve correctly without path duplication

Addresses code review feedback from chatgpt-codex-connector.
Addresses SonarQube security warning about using publicly writable directories
like /tmp/. The isOsLevelAbsolutePath() method only performs READ-ONLY path
string classification and does not perform any file operations.

Key security points documented:
- Method only does path pattern matching, no file I/O
- Actual file operations happen in ModuleLoader with proper validation
- Path classification prevents path traversal attacks by ensuring project-relative
  paths don't escape the baseUrl
- /tmp/ pattern is needed for test suite temp files (read-only classification)

This is a false positive - no files are created or modified in publicly writable
directories by this code.
@github-actions
Copy link
Copy Markdown

✅ Tests completed on Node.js 20.x: success

@Slashmsu Slashmsu requested a review from ggulpari November 17, 2025 15:06
Add NOSONAR suppression comment to the unixOsPrefixes array to silence the
security warning about publicly writable directories like /tmp/.

This is a confirmed false positive because:
- The array contains path PATTERNS for string matching only
- No file operations (create, write, read) are performed on these paths
- The patterns are used to classify whether an absolute path is OS-level
  or project-relative for module resolution
- Actual file I/O happens separately in ModuleLoader with proper validation

SonarQube's static analysis cannot distinguish between:
1. Unsafe: Creating/writing files in /tmp/ (security risk)
2. Safe: Pattern matching against '/tmp/' string (this code)

The NOSONAR comment is justified and documents the reason for suppression.
@sonarqubecloud
Copy link
Copy Markdown

@github-actions
Copy link
Copy Markdown

✅ Tests completed on Node.js 20.x: success

1 similar comment
@github-actions
Copy link
Copy Markdown

✅ Tests completed on Node.js 20.x: success

@ggulpari ggulpari merged commit 3a2cf4e into main Nov 17, 2025
7 checks passed
@ggulpari ggulpari deleted the claude/add-greeting-function-01CQtfagG9P8iXVRbbpKLKDp branch November 17, 2025 15:12
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.

2 participants