Re: [PATCH] gen_compile_commands.py: fix path resolve with symlinks in it

From: Nathan Chancellor
Date: Mon Dec 04 2023 - 11:59:25 EST


On Mon, Dec 04, 2023 at 06:41:42PM +0800, Jialu Xu wrote:
> When symbolic links are involved in the path, os.path.abspath might not
> resolve the symlinks and instead return the absolute path with the
> symlinks intact.

Can you provide an example or more detailed description of how this
behavior is currently broken? I can't really understand how having
symlinks in the path after normalization would break anything but I am
potentially missing something :)

> use pathlib.Path resolve() instead of os.path.abspath()
>
> Signed-off-by: Jialu Xu <xujialu@xxxxxxxxx>
> ---
> scripts/clang-tools/gen_compile_commands.py | 5 +++--
> 1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/scripts/clang-tools/gen_compile_commands.py b/scripts/clang-tools/gen_compile_commands.py
> index 180952fb91c1b..0a6c0996b4a8f 100755
> --- a/scripts/clang-tools/gen_compile_commands.py
> +++ b/scripts/clang-tools/gen_compile_commands.py
> @@ -11,6 +11,7 @@ import argparse
> import json
> import logging
> import os
> +from pathlib import Path
> import re
> import subprocess
> import sys
> @@ -172,8 +173,8 @@ def process_line(root_directory, command_prefix, file_path):
> # by Make, so this code replaces the escaped version with '#'.
> prefix = command_prefix.replace('\#', '#').replace('$(pound)', '#')
>
> - # Use os.path.abspath() to normalize the path resolving '.' and '..' .
> - abs_path = os.path.abspath(os.path.join(root_directory, file_path))
> + # Make the path absolute, resolving all symlinks on the way and also normalizing it.
> + abs_path = str(Path(os.path.join(root_directory, file_path)).resolve())

I think this can be more simply:

abs_path = str(Path(root_directory, file_path).resolve())

I think there should be a comment around why we are creating a Path
object then creating a string from it, rather than using the Path object
directly, namely that PosixPath is not JSON serializable.

> if not os.path.exists(abs_path):
> raise ValueError('File %s not found' % abs_path)
> return {
> --
> 2.39.2
>