## What this teaches

How to unexpected behaviour from S3 State Backend.

## The problem

Issue meltano/meltano#7938 (closed, 10 comments): ### Meltano Version

2.19.0

### Python Version

3.8

### Bug scope

API

### Operating System

Mac OS 13.4.1

### Description

Attempting to run a pipeline with S3 State Backend set.

Two strange issues occurred.

1. When I set `MELTANO_STATE_BACKEND_URI='s3://my-data-lake/state/` the files are saved to `s3://my-data-lake/state/state/[env]:[pipeline name]/`  so whatever subdirectory I use in my key is captured and reused as part of the key. It looks like the `/state/` part of my URI is correctly being parsed out as a prefix but is not being removed from the base URI...

## What worked (verified answer)

If Meltano's S3 state backend saves files under a duplicated path (s3://bucket/state/state/...), the bug only triggers when the MELTANO_STATE_BACKEND_URI ends with a trailing slash: the prefix is parsed out for reads and writes but not for deletes, so the subdirectory gets doubled. Short-term fix: remove the trailing slash from your state backend URI. The fix was merged upstream and the upgrade logic copies existing state files from the duplicated paths to the correct ones, so upgrading past it is safe.

## Source

gh:meltano/meltano#7938 - https://github.com/meltano/meltano/issues/7938
