Skip to content

fix: normalize filename and try to find a match#33

Merged
ramsey merged 6 commits intomainfrom
bug/file-name-case-sensitivity
Jan 14, 2022
Merged

fix: normalize filename and try to find a match#33
ramsey merged 6 commits intomainfrom
bug/file-name-case-sensitivity

Conversation

@ramsey
Copy link
Copy Markdown
Contributor

@ramsey ramsey commented Jan 13, 2022

We've been scratching our heads over an issue that appeared to work locally but never in sandbox, staging, or production environments.

Then, I had an ah-ha moment and tried this. First, on my Mac:

❯ docker run --rm -it -v $PWD:/app debian:bullseye sh -c 'ls -l /app/composer.json'
-rw-r--r-- 1 root root 23488 Dec 18 00:39 /app/composer.json

❯ docker run --rm -it -v $PWD:/app debian:bullseye sh -c 'ls -l /app/COMPOSER.JSON'
-rw-r--r-- 1 root root 23488 Dec 18 00:39 /app/COMPOSER.JSON

Then in a sandbox environment:

# ls -l composer.json
-rw-r--r-- 1 root root 23488 Jan 13 00:34 composer.json

# ls -l COMPOSER.JSON
ls: cannot access 'COMPOSER.JSON': No such file or directory

Yes, I know the macOS filesystem is case-insensitive, but even when using Linux containers, the directory mounted to the container IS STILL CASE-INSENSITIVE.

Our server environments are all Linux and, therefore, case-sensitive. 💥

The logic we are using to create the file path for looking up messages is, in short:

directory name + locale + ".json"

Since we're normalizing the locale name (i.e., en-xb and en_XB becomes en-XB), in a local environment, it works out great because en-XB.json and en-xb.json are the same files. However, in a server environment, since our locale files are lowercased (i.e., en-xb.json), it can't find these files, when running on the servers.

This PR normalizes the file names and searches through the files to find the correct one, comparing the normalized names, rather than concatenating the strings and hoping the files exist.

Fixes SK-36605

Product requirements and context

How has this been tested?

Tested in sandbox-3 by loading pages with different locales and confirmed this fixes the problem.

PR Checklist

  • I have added tests to cover my changes.

@ramsey ramsey requested a review from a team January 13, 2022 22:04
@ramsey
Copy link
Copy Markdown
Contributor Author

ramsey commented Jan 13, 2022

I'm not sure why Code Climate thinks the total coverage dropped. When I go to them for details, the page is empty.

@ramsey ramsey requested review from jrode and xiian January 13, 2022 22:06
);
}
$options->weekday = ['short', 'long', 'narrow', 'short'][$length - 4];
$options->weekday = $this->getWeekdayValue($length - 4);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This fixes a static analysis issue that Psalm raised in CI.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Okay because I don't understand what a narrow weekday is :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

These values are the ECMA-402 names that translate to ICU formatting symbols. "Narrow" basically means the shortest possible display. So, while long is "Friday" and short is "Fri," narrow is "F"

Comment thread src/MessageLoader.php
return $this->formatHelper->getReader($formatReader);
}

private function getFilePathForLocale(string $locale): string
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This method fixes the case-sensitivity problem.

Comment thread src/MessageLoader.php
{
$normalize = fn (string $filename): string => str_replace('_', '-', strtolower($filename));
$searchFile = $normalize($locale . self::MESSAGE_FILE_EXTENSION);
$localeFiles = scandir($this->messagesDirectory, SCANDIR_SORT_NONE) ?: [];
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Would it be worth putting in a short-circuit before the scandir in case the file can be found directly?

Just thinking of cases where messagesDirectory might have a bunch of files in it and everything is named (and cased) properly, so we can save the disk IO.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good call!

Comment thread CHANGELOG.md

### Fixed

- Normalize the locale file name before searching for it in `MessageLoader`, to account for differences in case, as well as filesystem case sensitivity (e.g. "en-XB" vs. "en_xb")
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Did you mean (e.g. "en-XB" vs. "en-xb") (both hiphens)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I meant en_xb. Even if someone names their locale files EN_XB.json, EN-XB.json, EN-xb.json, etc., the locale en-XB should match.

Comment thread src/MessageLoader.php

private function getFilePathForLocale(string $locale): string
{
$normalize = fn (string $filename): string => str_replace('_', '-', strtolower($filename));
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍

Copy link
Copy Markdown

@jrode jrode left a comment

Choose a reason for hiding this comment

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

Looks great, nice catch. One non-blocker.

@qlty-cloud-legacy
Copy link
Copy Markdown

Code Climate has analyzed commit c38f784 and detected 0 issues on this pull request.

The test coverage on the diff in this pull request is 90.0% (80% is the threshold).

This pull request will bring the total coverage in the repository to 96.8% (-0.1% change).

View more on Code Climate.

@ramsey ramsey merged commit e7decc6 into main Jan 14, 2022
@ramsey ramsey deleted the bug/file-name-case-sensitivity branch January 14, 2022 17:38
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