From d1297f8dbf588351e55f117ba853d399d58e5c12 Mon Sep 17 00:00:00 2001 From: Anjali Ratnam Date: Mon, 8 Jun 2026 14:00:32 -0700 Subject: [PATCH] fixed ls() bug of files with the same prefix --- CHANGELOG.md | 2 +- adlfs/spec.py | 17 ++++++++++------- adlfs/tests/test_spec.py | 17 +++++++++++++++++ 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b0ced3c7..e889912c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ Unreleased ---------- -- . +- Fixed `ls()` returning incorrect results when a file shares the same prefix as other files in the same directory (#409) 2026.5.0 -------- diff --git a/adlfs/spec.py b/adlfs/spec.py index 31a26121..bcc42a3e 100644 --- a/adlfs/spec.py +++ b/adlfs/spec.py @@ -946,13 +946,16 @@ async def _details( data["name"] = data["name"].rstrip("/") output.append(data) if target_path: - if ( - len(output) == 1 - and output[0]["type"] == "file" - and not self.version_aware - ): - # This handles the case where path is a file passed to ls - return output + if not self.version_aware: + # This handles the case where a file path is passed to ls and there are + # multiple files with the same prefix. + file = [ + o + for o in output + if o["name"] == target_path and o["type"] == "file" + ] + if file: + return file output = await filter_blobs( output, target_path, diff --git a/adlfs/tests/test_spec.py b/adlfs/tests/test_spec.py index d2a63dee..7d4ab3e2 100644 --- a/adlfs/tests/test_spec.py +++ b/adlfs/tests/test_spec.py @@ -2610,3 +2610,20 @@ def test_etag_normalized_form(storage): ) def test_striping_etag(input_etag, expected_etag): assert _normalize_etag_quotes(input_etag) == expected_etag + + +def test_ls_files_with_same_prefix(storage): + fs = AzureBlobFileSystem( + account_name=storage.account_name, + connection_string=CONN_STR, + ) + path1 = "data/test/file.txt" + path2 = "data/test/file.txt.1" + fs.touch(path1) + fs.touch(path2) + + assert fs.ls("data/test/") == [path1, path2] + assert fs.ls("data/test/file.txt") == [path1] + assert fs.ls("data/test/file.txt.1") == [path2] + + fs.rm("data/test", recursive=True)