Skip to content

LS function fix for single file input #1106

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

Merged
merged 5 commits into from
Jun 3, 2025

Conversation

rishabhmalikMS
Copy link
Contributor

@rishabhmalikMS rishabhmalikMS commented May 29, 2025

Explanation of the fix we implemented in the ls function for the single file input issue:

Issue

  • When providing a single file as input to ls(), it was returning an empty string because the relative path calculation was comparing the file path against itself.

Fix Details

  • We separated the handling of files and directories into two phases:
    // Phase 1: Early file handling
    if (stats.isFile()) {
        // For files, just add the basename to results array
        const fileName = path.basename(pathEntry);
        results.push(fileName);
    } else {
        // Keep directories for later processing
        remainingPaths.push(pathEntry);
    }

Key Changes

  1. Files are processed first, before any directory traversal
  2. For files, we simply extract and store the basename
  3. Only directories continue to the existing relative path calculation logic

Benefits

  1. No changes to existing directory handling code
  2. Maintains backward compatibility
  3. Fixes the single file issue in ArchiveFiles@2 task
  4. Clearer separation between file and directory processing

Example

- task: ArchiveFiles@2
  inputs:
    rootFolderOrFile: 'path/to/single/file.txt'

Now correctly adds the file to the archive with its proper name instead of an empty string.

Link to modified canary pipeline with specific file archiving tests.

This pipeline run is from my private organization which is run on archive file task linked locally to modified task-lib code. This includes new tests named Test archive with specific file zip for testing the change
https://dev.azure.com/rishabhmalikOrg/Test%20Project/_build/results?buildId=1040&view=logs&j=1809f88b-43e1-5a25-3102-9dca9646ed63

@rishabhmalikMS rishabhmalikMS requested review from a team as code owners May 29, 2025 04:55
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@rishabhmalikMS rishabhmalikMS changed the title Users/rishabhmalik ms/ls fix LS function fix for single file May 29, 2025
@rishabhmalikMS rishabhmalikMS changed the title LS function fix for single file LS function fix for single file input May 29, 2025
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@rishabhmalikMS
Copy link
Contributor Author

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@raujaiswal
Copy link

raujaiswal commented May 30, 2025

After running npm install with Node.js version v20.19.1, there are changes detected in the package-lock.json file. Could you please review these changes and confirm whether they are necessary to commit?

image

@rishabhmalikMS
Copy link
Contributor Author

After running npm install with Node.js version v20.19.1, there are changes detected in the package-lock.json file. Could you please review these changes and confirm whether they are necessary to commit?

image

These changes are mostly due to dependencies version being specified with ^

@rishabhmalikMS rishabhmalikMS merged commit 740206e into master Jun 3, 2025
6 checks passed
@raujaiswal raujaiswal requested a review from Copilot June 4, 2025 10:21
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes an issue where providing a single file as input to the ls() function returned an empty string. It introduces a two-phase processing approach that first handles files (extracting basenames) and then processes directories, and it updates the array flattening logic along with a minor version bump.

  • Fix for single file input in ls() to correctly return file basenames.
  • Separation of file and directory handling with dedicated processing logic.
  • Introduces a custom flattenArray function for array flattening.

Reviewed Changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 1 comment.

File Description
node/test/ls.ts Adds a test to verify that single file paths are correctly handled.
node/task.ts Updates the ls() function to handle files separately and replaces Array.prototype.flat with a custom flattenArray function.
node/package.json Bumps package version to reflect the updates.
Files not reviewed (1)
  • node/package-lock.json: Language not supported
Comments suppressed due to low confidence (1)

node/task.ts:1143

  • Consider adding a brief comment above flattenArray to document its purpose and recursive behavior, which can aid in future maintainability.
function flattenArray(arr: any[]): any[] {

Comment on lines +1078 to +1079
while (paths.length > 0) {
const pathEntry = resolve(paths.shift());
Copy link
Preview

Copilot AI Jun 4, 2025

Choose a reason for hiding this comment

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

Using paths.shift() repeatedly in a loop can lead to suboptimal performance for large arrays; consider refactoring to an index-based loop to improve efficiency.

Suggested change
while (paths.length > 0) {
const pathEntry = resolve(paths.shift());
for (let i = 0; i < paths.length; i++) {
const pathEntry = resolve(paths[i]);

Copilot uses AI. Check for mistakes.

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.

3 participants