Skip to content
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

Relative layer paths incorrectly resolve to matching filenames in the working directory on project save #60100

Open
2 tasks done
vsydorov opened this issue Jan 9, 2025 · 1 comment
Labels
Bug Either a bug report, or a bug fix. Let's hope for the latter! Project

Comments

@vsydorov
Copy link

vsydorov commented Jan 9, 2025

What is the bug or the crash?

Summary: When saving a QGIS project, if a file with the same name as a layer's datasource exists in the current working directory, the layer's datasource is silently replaced. The layer then points to the file in the working directory instead of its original location.

The issue is caused by these two lines of code in qgsmaplayer.cpp:

const QString srcRaw = encodedSource( source(), context );
const QString src = providerKey.isEmpty() ? srcRaw : QgsProviderRegistry::instance()->absoluteToRelativeUri( providerKey, srcRaw, context );

The encodedSource method at Line 782 (whether it resolves to QgsRasterLayer::encodedSource, QgsVectorLayer::encodedSource, QgsPointCloudLayer::encodedSource or other) already calls to QgsProviderRegistry::instance()->absoluteToRelativeUri. As a result, the second call on line 783 is redundant. This leads to the QgsPathResolver::writePath method being called on on a path that is already relative, see here:

const QFileInfo srcFileInfo( srcPath );
if ( srcFileInfo.exists() )
srcPath = srcFileInfo.canonicalFilePath();

The srcFileInfo.canonicalFilePath() call resolves the path relative to the current working directory, not the project directory. This creates a problem when the working directory contains files with the same name as the layer datasources:

  • Windows users: The working directory is typically set to C:/Windows/System32 when QGIS is launched from the system menu. This reduces the likelihood of the issue occurring.
  • Linux users: The working directory is usually the user's home directory (/home/username). This directory often contains various files, increasing the chance of conflicts.
  • The working project directory in QGIS can be queried by a simple QDir::currentPath() call. To verify it in the running application, one can simply query QDir.currentPath() in the Python console, or Path('.').resolve().

Example scenario:

  • A Linux user has a file named points.geojson in their home directory.
  • They open a QGIS project located in /home/username/projects/demo/ that includes a relative datasource ./points.geojson.
  • When the project is saved, the datasource path is incorrectly resolved to ../../points.geojson, pointing to /home/username/points.geojson instead of the correct /home/username/projects/demo/points.geojson.
  • This happens silently during the save operation, leading to unexpected behavior.

Steps to reproduce the issue

  1. Create a directory structure with two subfolders: WORKDIR and PROJDIR, both containing a file named points.geojson.
  2. Create a new QGIS project in PROJDIR with a layer referencing ./points.geojson in that folder, save the project, and quit QGIS.
  3. Launch QGIS with the working directory set to WORKDIR. On Linux, run QGIS from WORKDIR. On Windows, you can set the working directory by running QDir.setCurrent('/path/to/WORKDIR/) in the Python console.
  4. Open the QGIS project in PROJDIR, save the project.
  5. Observe that the layer's datasource path has changed to reference ../WORKDIR/points.geojson instead of the correct ./points.geojson in PROJDIR.

Versions

Verified on the 8e7781d dev version (3.41.0-Master) and on 3.40.2-Bratislava. Here's the version paste.

QGIS version 3.41.0-Master 3.40.2-Bratislava
QGIS code revision 8e7781d 14826ca
Libraries
Qt version 5.15.13 5.15.13
Python version 3.12.3 3.12.3
GDAL/OGR version 3.9.3 3.9.3
PROJ version 9.4.1 9.4.1
EPSG Registry database version v11.006 (2024-03-13) v11.006 (2024-03-13)
GEOS version 3.12.2-CAPI-1.18.2 3.12.2-CAPI-1.18.2
SQLite version 3.45.1 3.45.1
PDAL version 2.6.2 2.6.2
PostgreSQL client version unknown 16.6 (Ubuntu 16.6-0ubuntu0.24.04.1)
SpatiaLite version 5.1.0 5.1.0
QWT version 6.1.4 6.1.4
QScintilla2 version 2.14.1 2.14.1
OS version Ubuntu 24.04.1 LTS Ubuntu 24.04.1 LTS
This copy of QGIS writes debugging output.
Active Python plugins
wbt_for_qgis 1.0.9 1.0.9
LAStools 2.1.1 2.1.1
quick_map_services 0.19.36 0.19.36
rvt-qgis 0.9.6 0.9.6
SRTM-Downloader 3.2.3 3.2.3
grassprovider 2.12.99 2.12.99
processing 2.12.99 2.12.99
MetaSearch 0.3.6 0.3.6

Supported QGIS version

  • I'm running a supported QGIS version according to the roadmap.

New profile

Additional Context

This issue is less likely to occur on Windows due to the default working directory. However, it is common in Linux workflows, when QGIS is launched from the user's home directory.

I have identified and fixed the issue in my local setup and will submit a pull request targeting 3.41.0-Master.

@vsydorov vsydorov added the Bug Either a bug report, or a bug fix. Let's hope for the latter! label Jan 9, 2025
@vsydorov
Copy link
Author

This issue seems related too: #59282

vsydorov added a commit to vsydorov/QGIS that referenced this issue Jan 10, 2025
- The b9c1c2c commit introduced redundant absoluteToRelativeUri AND
  relativeToAbsoluteUri calls. First one was removed earlier, stopping
  the qgis#60100 issue. Remove the latter for completeness too.
- Minor indentation fix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Either a bug report, or a bug fix. Let's hope for the latter! Project
Projects
None yet
Development

No branches or pull requests

2 participants